<project ……> …… <dependencies> …… <dependency> <!-- [MyBatis 3]. --> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.8</version> </dependency> </dependencies> </project>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-config.dtd">
<configuration>
<!-- Connection and transaction parameters: -->
<environments default="environment">
<environment id="environment">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.OracleDriver"/>
<property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:demo01"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
</dataSource>
</environment>
</environments>
<!-- Mappers: No mapper added currently.
<mappers>
<mapper resource=".../.../XXXMapping.xml"/>
<mapper resource=".../.../YYYMapping.xml"/>
</mappers>
-->
</configuration>
……
public class TestCase
{
private String strConfFile =
"src/main/resources/tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/mybatis-config.xml";
@Test
public void testObjects() throws IOException
{
SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder();
System.out.println("SqlSessionFactoryBuilder:\n\t" + ssfb);
System.out.println("Configuration File:\n\t" + strConfFile);
InputStream isMyBatisCfg = new FileInputStream(strConfFile);
System.out.println("Configuration InputStream:\n\t" + isMyBatisCfg);
SqlSessionFactory ssf = ssfb.build(isMyBatisCfg);
System.out.println("SqlSessionFactory:\n\t" + ssf);
isMyBatisCfg.close();
System.out.println(
"Configuration InputStream Closed:\n\t" + isMyBatisCfg);
SqlSession session = ssf.openSession();
System.out.println("SqlSession:\n\t" + session);
session.close();
System.out.println("SqlSession Closed:\n\t" + session);
}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="crud.mybatis.user">
<!--
<select id="create"/></select>
<select id="findById"/></select>
<select id="listByName"/></select>
<select id="listAll"/></select>
<select id="listByRange"/></select>
<update id="update"/></update>
<update id="discardById"/></update>
-->
</mapper>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <configuration> <!-- Connection and transaction parameters: --> <environments ……> …… </environments> <!-- Mappers: --> <mappers> <!-- <mapper resource=".../.../XXXMapping.xml"/> <mapper resource=".../.../YYYMapping.xml"/> --> <mapper resource="tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/_01_manual_dao_way/dao/impl/oracle/User.mybatis_mapping.xml"/> </mappers> </configuration>
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper namespace="crud.mybatis.user"> <insert id="create" parameterType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> select tfw_seq_dash_user_id.nextval from dual </selectKey> insert into tfw_dash_user( id, name, passwd, stat, remark ) values( #{id, jdbcType=INTEGER}, #{name, jdbcType=VARCHAR}, #{passwd,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{remark,jdbcType=VARCHAR} ) </insert> <!-- <select id="findById"/></select> …… --> </mapper>
…… public class _01_MyBatis_Operation { private String strConfFile = "src/main/resources/tfw/integration_demo/_04_spring_web_mvc/_02_crud/_03_mybatis_crud/mybatis-config.xml"; private SqlSession session; private final String MYBATIS_NAMESPACE = "crud.mybatis.user"; @Test public void testObjects() throws IOException { …… } @Before public void init() throws IOException { SqlSessionFactoryBuilder ssfb = new SqlSessionFactoryBuilder(); System.out.println("SqlSessionFactoryBuilder:\n\t" + ssfb); System.out.println("Configuration File:\n\t" + strConfFile); InputStream isMyBatisCfg = new FileInputStream(strConfFile); System.out.println("Configuration InputStream:\n\t" + isMyBatisCfg); SqlSessionFactory ssf = ssfb.build(isMyBatisCfg); System.out.println("SqlSessionFactory:\n\t" + ssf); isMyBatisCfg.close(); System.out.println( "Configuration InputStream Closed:\n\t" + isMyBatisCfg); session = ssf.openSession(); System.out.println("SqlSession:\n\t" + session); } @After public void close() { session.close(); System.out.println("SqlSession Closed:\n\t" + session); } @Test public void testMyBatis_create() { // [S] Preparing. User user; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "create"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); String strDate = DateToolE.simpleFormat(new Date(), "HH-mm-ss.SSS"); String strPasswd = null; // "Password"; user = // null; // [!] May fail due to database constraints. new User(null, "MyBatis_N_" + strDate, strPasswd, new Byte((byte) 0), "MyBatis_R_" + strDate); System.out.println("User:\n\t" + user); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, user); System.out.println(strSql); } // [E] Preparing. // [S] Main. { int intRtnCode = session.insert(MYBATIS_OPERATION, user); System.out.println("Return Code:\n\t" + intRtnCode); session.commit(); System.out.println("User:\n\t" + user); } // [E] Main. } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> <insert id="create" ……> …… </insert> <insert id="createAfter" parameterType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> insert into tfw_dash_user( id, name, passwd, stat, remark ) values( tfw_seq_dash_user_id.nextval, #{name, jdbcType=VARCHAR}, #{passwd,jdbcType=VARCHAR}, #{status,jdbcType=TINYINT}, #{remark,jdbcType=VARCHAR} ) <selectKey keyProperty="id" resultType="int" order="AFTER"> select tfw_seq_dash_user_id.currval from dual </selectKey> </insert> <!-- <select id="findById"/></select> …… --> </mapper>
…… public class _01_MyBatis_Operation { …… @Test public void testMyBatis_create() { …… } @Test public void testMyBatis_createAfter() { // [S] Preparing. User user; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "createAfter"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); String strDate = DateToolE.simpleFormat(new Date(), "HH-mm-ss.SSS"); String strPasswd = null; // "Password"; user = // null; // [!] May fail due to database constraints. new User(null, "MyBatis_N_" + strDate, strPasswd, new Byte((byte) 0), "MyBatis_R_" + strDate); System.out.println("User:\n\t" + user); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, user); System.out.println(strSql); } // [E] Preparing. // [S] Main. { int intRtnCode = session.insert(MYBATIS_OPERATION, user); System.out.println("Return Code:\n\t" + intRtnCode); session.commit(); System.out.println("User:\n\t" + user); } // [E] Main. } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> …… <insert id="createAfter" ……> …… </insert> <select id="findById" parameterType="java.lang.Integer" resultType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> select id, name, passwd, stat as status, remark from tfw_dash_user where id <!-- =#{AbCdEfG,jdbcType=INTEGER} --> <choose> <when test="_parameter!=null">=#{AbCdEfG}</when> <otherwise> is null</otherwise> </choose> </select> <!-- <select id="listByName"/></select> …… --> </mapper>
…… public class _01_MyBatis_Operation { …… @Test public void testMyBatis_createAfter() { …… } @Test public void testMyBatis_findById() { // [S] Preparing. Integer itgUserId; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "findById"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); itgUserId = // null; new Integer(10000); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, itgUserId); System.out.println(strSql); } // [E] Preparing. // [S] Main. { User user = session.selectOne(MYBATIS_OPERATION, itgUserId); System.out.println("User:\n\t" + user); List<User> lstUsers = session.selectList(MYBATIS_OPERATION, itgUserId); showUsers(lstUsers); } // [E] Main. } private void showUsers(List<User> lstUsers) { …… } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> …… <select id="findById" …… ……> …… </select> <select id="listByName" parameterType="string" resultType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> select id, name, passwd, stat as status, remark from tfw_dash_user where name <choose> <when test="_parameter!=null">=#{AbCdEfG}</when> <otherwise> is null</otherwise> </choose> order by name, id </select> <!-- <select id="listAll"/></select> …… --> </mapper>
…… public class _01_MyBatis_Operation { …… @Test public void testMyBatis_findById() { …… } @Test public void testMyBatis_listByName() { // [S] Preparing. String strUserName; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "listByName"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); strUserName = // null; // "Name_AAA"; "root"; String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, strUserName); System.out.println(strSql); } // [E] Preparing. // Main. List<User> lstUsers = session.selectList(MYBATIS_OPERATION, strUserName); showUsers(lstUsers); } private void showUsers(List<User> lstUsers) { …… } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> …… <select id="listByName" …… ……> …… </select> <select id="listAll" resultType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> select id, name, passwd, stat as status, remark from tfw_dash_user order by name, id </select> <!-- <select id="listByRange"/></select> …… --> </mapper>
…… public class _01_MyBatis_Operation { …… public void testMyBatis_listByName() { …… } @Test public void testMyBatis_listAll() { // [S] Preparing. String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "listAll"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION); System.out.println(strSql); } // [E] Preparing. // Main. List<User> lstUsers = session.selectList(MYBATIS_OPERATION); showUsers(lstUsers); } private void showUsers(List<User> lstUsers) { …… } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> …… <select id="listAll" ……> …… </select> <select id="listByRange" parameterType="map" resultType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> select id, name, passwd, stat as status, remark from ( select rownum rn, t.* from ( select id, name, passwd, stat, remark from tfw_dash_user order by name, id ) t where rownum<=(#{start_row_index}+#{select_row_count}) ) where rn>=(#{start_row_index}+1) </select> <!-- <select id="update"/></select> …… --> </mapper>
…… public class _01_MyBatis_Operation { …… public void testMyBatis_listAll() { …… } @Test public void testMyBatis_listByRange() { // [S] Preparing. Map<String, Integer> mapRange; List<User> lstUsers; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "listByRange"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); lstUsers = session.selectList(MYBATIS_NAMESPACE + ".listAll"); showUsers(lstUsers); System.out.println("----------------------------------------"); mapRange = new HashMap<String, Integer>(); mapRange.put("start_row_index", 15); mapRange.put("select_row_count", 10); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, mapRange); System.out.println(strSql); } // [E] Preparing. // Main. lstUsers = session.selectList(MYBATIS_OPERATION, mapRange); showUsers(lstUsers); } private void showUsers(List<User> lstUsers) { …… } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> …… <select id="listByRange" …… …… …… </select> <update id="update" parameterType="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.entity.User"> <choose> <when test="_parameter!=null"> update tfw_dash_user set name =#{name, jdbcType=VARCHAR}, passwd=#{passwd,jdbcType=VARCHAR}, stat =#{status,jdbcType=TINYINT}, remark=#{remark,jdbcType=VARCHAR} where id <choose> <when test="id!=null">=#{id}</when> <otherwise> is null</otherwise> </choose> </when> <otherwise>select -1 from dual</otherwise> </choose> </update> <!-- <select id="discardById"/></select> --> </mapper>
…… public class _01_MyBatis_Operation { …… public void testMyBatis_listByRange() { …… } @Test public void testMyBatis_update() { // [S] Preparing. Integer itgUserId; User user; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "update"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); itgUserId = // null; 10017; // 99999; user = session.selectOne(MYBATIS_NAMESPACE + ".findById", itgUserId); System.out.println("User (before):\n\t" + user); String strDate = DateToolE.simpleFormat(new Date(), "yyyy-MM-ss_HH-mm-ss.SSS"); String strPasswd = // null; "passwd"; user = // null; new User(itgUserId, "Name_" + strDate, strPasswd, (byte) 2, "Remark_" + strDate); System.out.println("User (new):\n\t" + user); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, user); System.out.println(strSql); } // [E] Preparing. // Main. int intRtnCode = session.update(MYBATIS_OPERATION, user); System.out.println("Return Code:\n\t" + intRtnCode); session.commit(); // Confirmation. user = session.selectOne(MYBATIS_NAMESPACE + ".findById", itgUserId); System.out.println("User (updated):\n\t" + user); } private void showUsers(List<User> lstUsers) { …… } }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE ……> <mapper ……> …… <update …… ……> …… </update> <update id="discardById" parameterType="int"> update tfw_dash_user set stat=-1 where id <choose> <when test="_parameter!=null">=#{AbCdEfG}</when> <otherwise> is null</otherwise> </choose> </update> </mapper>
…… public class _01_MyBatis_Operation { …… public void testMyBatis_update() { …… } @Test public void testMyBatis_discardById() { // [S] Preparing. Integer itgUserId; User user; String MYBATIS_OPERATION; { final String MYBATIS_OPERATION_ID = "discardById"; MYBATIS_OPERATION = MYBATIS_NAMESPACE + "." + MYBATIS_OPERATION_ID; System.out.println(MYBATIS_OPERATION); itgUserId = // null; 10017; // 99999; user = session.selectOne(MYBATIS_NAMESPACE + ".findById", itgUserId); System.out.println("User (before):\n\t" + user); String strSql = MyBatisSqlHelper.getNamespaceSql(session, MYBATIS_OPERATION, itgUserId); System.out.println(strSql); } // [E] Preparing. // Main. int intRtnCode = session.update(MYBATIS_OPERATION, itgUserId); System.out.println("Return Code:\n\t" + intRtnCode); session.commit(); // Confirmation. user = session.selectOne(MYBATIS_NAMESPACE + ".findById", itgUserId); System.out.println("User (discarded):\n\t" + user); } private void showUsers(List<User> lstUsers) { …… } }