简介
学习1中采用以前ibatis常用方法,这里采用利用抽象接口更加灵活的方式来解决数据库增删改查。
依赖文件
没有变化
数据库全局配置文件
mybatis-mysql.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?serverTimezone=CTT&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true"/>
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="EmployeeMapper.xml"/>
<mapper resource="EmployeeMapperInterface"/>
</mappers>
</configuration>
接口对应的xml文件
EmployeeMapperInterface
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.EmployeeMapper">
<!--
namespace: 命名空间, 与接口对应
id: 位移标识, 必须与接口定义的方法一致
resultType: 返回数据类型
-->
<select id="getEmpById" resultType="com.mybatis.bean.Employee">
select * from tbl_employee where id = #{id}
</select>
</mapper>
dao 接口定义
package com.mybatis.dao;
import com.mybatis.bean.Employee;
/**
* @author : kean
* @version V1.0
* @Project: springboot2
* @Package com.mybatis.dao
* @Description: TODO
* @date Date : 2019-05-04 13:05
*/
public interface EmployeeMapper {
public Employee getEmpById(Integer id);
}
Junit 测试文件
package com.mybatis;
import com.mybatis.bean.Employee;
import com.mybatis.dao.EmployeeMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
/**
* @author : kean
* @version V1.0
* @Project: springboot2
* @Package com.mybatis
* @Description: TODO
* @date Date : 2019-05-03 22:59
*/
public class MybatisTest {
private static final Logger logger = LoggerFactory.getLogger(MybatisTest.class);
private static final String resource = "mybatis-mysql.xml";
/**
* xml SqlSessionFactory 创建
*
* @throws IOException
*/
@Test
public void test() throws IOException {
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Employee employee = sqlSession.selectOne("com.mybatis.bean.EmployeeMapper.selectEmp", 101);
logger.info("{} {} {}", employee.getId(), employee.getName(), employee.getSex());
} finally {
sqlSession.close();
logger.info("查询完成!");
}
}
@Test
public void test2() throws IOException {
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);
logger.info("Mapper class: {}", employeeMapper.getClass());
Employee employee = employeeMapper.getEmpById(101);
logger.info("{} {} {}", employee.getId(), employee.getName(), employee.getSex());
} finally {
sqlSession.close();
logger.info("查询完成!");
}
}
}
测试结果
D:\apps\jdk\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\apps\IntelliJ IDEA 2019.1\lib\idea_rt.jar=7292:D:\apps\IntelliJ IDEA 2019.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\apps\IntelliJ IDEA 2019.1\lib\idea_rt.jar;D:\apps\IntelliJ IDEA 2019.1\plugins\junit\lib\junit-rt.jar;D:\apps\IntelliJ IDEA 2019.1\plugins\junit\lib\junit5-rt.jar;D:\apps\jdk\jre\lib\charsets.jar;D:\apps\jdk\jre\lib\deploy.jar;D:\apps\jdk\jre\lib\ext\access-bridge-64.jar;D:\apps\jdk\jre\lib\ext\cldrdata.jar;D:\apps\jdk\jre\lib\ext\dnsns.jar;D:\apps\jdk\jre\lib\ext\jaccess.jar;D:\apps\jdk\jre\lib\ext\jfxrt.jar;D:\apps\jdk\jre\lib\ext\localedata.jar;D:\apps\jdk\jre\lib\ext\nashorn.jar;D:\apps\jdk\jre\lib\ext\sunec.jar;D:\apps\jdk\jre\lib\ext\sunjce_provider.jar;D:\apps\jdk\jre\lib\ext\sunmscapi.jar;D:\apps\jdk\jre\lib\ext\sunpkcs11.jar;D:\apps\jdk\jre\lib\ext\zipfs.jar;D:\apps\jdk\jre\lib\javaws.jar;D:\apps\jdk\jre\lib\jce.jar;D:\apps\jdk\jre\lib\jfr.jar;D:\apps\jdk\jre\lib\jfxswt.jar;D:\apps\jdk\jre\lib\jsse.jar;D:\apps\jdk\jre\lib\management-agent.jar;D:\apps\jdk\jre\lib\plugin.jar;D:\apps\jdk\jre\lib\resources.jar;D:\apps\jdk\jre\lib\rt.jar;D:\java_workspace\springboot2\target\test-classes;D:\java_workspace\springboot2\target\classes;D:\repository\org\springframework\boot\spring-boot-starter\2.1.0.RELEASE\spring-boot-starter-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot\2.1.0.RELEASE\spring-boot-2.1.0.RELEASE.jar;D:\repository\org\springframework\spring-context\5.1.2.RELEASE\spring-context-5.1.2.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-autoconfigure\2.1.0.RELEASE\spring-boot-autoconfigure-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-logging\2.1.0.RELEASE\spring-boot-starter-logging-2.1.0.RELEASE.jar;D:\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\repository\org\apache\logging\log4j\log4j-to-slf4j\2.11.1\log4j-to-slf4j-2.11.1.jar;D:\repository\org\apache\logging\log4j\log4j-api\2.11.1\log4j-api-2.11.1.jar;D:\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\repository\org\springframework\spring-core\5.1.2.RELEASE\spring-core-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-jcl\5.1.2.RELEASE\spring-jcl-5.1.2.RELEASE.jar;D:\repository\org\yaml\snakeyaml\1.23\snakeyaml-1.23.jar;D:\repository\org\springframework\boot\spring-boot-starter-test\2.1.0.RELEASE\spring-boot-starter-test-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-test\2.1.0.RELEASE\spring-boot-test-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-test-autoconfigure\2.1.0.RELEASE\spring-boot-test-autoconfigure-2.1.0.RELEASE.jar;D:\repository\com\jayway\jsonpath\json-path\2.4.0\json-path-2.4.0.jar;D:\repository\net\minidev\json-smart\2.3\json-smart-2.3.jar;D:\repository\net\minidev\accessors-smart\1.2\accessors-smart-1.2.jar;D:\repository\org\ow2\asm\asm\5.0.4\asm-5.0.4.jar;D:\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\repository\org\assertj\assertj-core\3.11.1\assertj-core-3.11.1.jar;D:\repository\org\mockito\mockito-core\2.23.0\mockito-core-2.23.0.jar;D:\repository\net\bytebuddy\byte-buddy\1.9.3\byte-buddy-1.9.3.jar;D:\repository\net\bytebuddy\byte-buddy-agent\1.9.3\byte-buddy-agent-1.9.3.jar;D:\repository\org\objenesis\objenesis\2.6\objenesis-2.6.jar;D:\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\repository\org\hamcrest\hamcrest-library\1.3\hamcrest-library-1.3.jar;D:\repository\org\skyscreamer\jsonassert\1.5.0\jsonassert-1.5.0.jar;D:\repository\com\vaadin\external\google\android-json\0.0.20131108.vaadin1\android-json-0.0.20131108.vaadin1.jar;D:\repository\org\springframework\spring-test\5.1.2.RELEASE\spring-test-5.1.2.RELEASE.jar;D:\repository\org\xmlunit\xmlunit-core\2.6.2\xmlunit-core-2.6.2.jar;D:\repository\org\projectlombok\lombok\1.18.4\lombok-1.18.4.jar;D:\repository\org\springframework\boot\spring-boot-starter-web\2.1.0.RELEASE\spring-boot-starter-web-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-json\2.1.0.RELEASE\spring-boot-starter-json-2.1.0.RELEASE.jar;D:\repository\com\fasterxml\jackson\core\jackson-databind\2.9.7\jackson-databind-2.9.7.jar;D:\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\repository\com\fasterxml\jackson\core\jackson-core\2.9.7\jackson-core-2.9.7.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.7\jackson-datatype-jdk8-2.9.7.jar;D:\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.7\jackson-datatype-jsr310-2.9.7.jar;D:\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.7\jackson-module-parameter-names-2.9.7.jar;D:\repository\org\springframework\boot\spring-boot-starter-tomcat\2.1.0.RELEASE\spring-boot-starter-tomcat-2.1.0.RELEASE.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-core\9.0.12\tomcat-embed-core-9.0.12.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-el\9.0.12\tomcat-embed-el-9.0.12.jar;D:\repository\org\apache\tomcat\embed\tomcat-embed-websocket\9.0.12\tomcat-embed-websocket-9.0.12.jar;D:\repository\org\hibernate\validator\hibernate-validator\6.0.13.Final\hibernate-validator-6.0.13.Final.jar;D:\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\repository\com\fasterxml\classmate\1.4.0\classmate-1.4.0.jar;D:\repository\org\springframework\spring-web\5.1.2.RELEASE\spring-web-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-beans\5.1.2.RELEASE\spring-beans-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-webmvc\5.1.2.RELEASE\spring-webmvc-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-expression\5.1.2.RELEASE\spring-expression-5.1.2.RELEASE.jar;D:\repository\mysql\mysql-connector-java\8.0.13\mysql-connector-java-8.0.13.jar;D:\repository\org\springframework\boot\spring-boot-starter-jdbc\2.1.0.RELEASE\spring-boot-starter-jdbc-2.1.0.RELEASE.jar;D:\repository\com\zaxxer\HikariCP\3.2.0\HikariCP-3.2.0.jar;D:\repository\org\springframework\spring-jdbc\5.1.2.RELEASE\spring-jdbc-5.1.2.RELEASE.jar;D:\repository\org\springframework\spring-tx\5.1.2.RELEASE\spring-tx-5.1.2.RELEASE.jar;D:\repository\org\mybatis\spring\boot\mybatis-spring-boot-starter\1.3.2\mybatis-spring-boot-starter-1.3.2.jar;D:\repository\org\mybatis\spring\boot\mybatis-spring-boot-autoconfigure\1.3.2\mybatis-spring-boot-autoconfigure-1.3.2.jar;D:\repository\org\mybatis\mybatis-spring\1.3.2\mybatis-spring-1.3.2.jar;D:\repository\org\apache\commons\commons-dbcp2\2.5.0\commons-dbcp2-2.5.0.jar;D:\repository\org\apache\commons\commons-pool2\2.6.0\commons-pool2-2.6.0.jar;D:\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar;D:\repository\org\springframework\boot\spring-boot-configuration-processor\2.1.0.RELEASE\spring-boot-configuration-processor-2.1.0.RELEASE.jar;D:\repository\org\springframework\boot\spring-boot-starter-aop\2.1.0.RELEASE\spring-boot-starter-aop-2.1.0.RELEASE.jar;D:\repository\org\springframework\spring-aop\5.1.2.RELEASE\spring-aop-5.1.2.RELEASE.jar;D:\repository\org\aspectj\aspectjweaver\1.9.2\aspectjweaver-1.9.2.jar;D:\repository\org\mybatis\mybatis\3.4.6\mybatis-3.4.6.jar;D:\repository\junit\junit\4.12\junit-4.12.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.mybatis.MybatisTest,test2
13:17:26.227 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
13:17:26.366 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
13:17:26.366 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
13:17:26.366 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
13:17:26.366 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - PooledDataSource forcefully closed/removed all connections.
13:17:26.491 [main] INFO com.mybatis.MybatisTest - Mapper class: class com.sun.proxy.$Proxy5
13:17:26.507 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.
13:17:28.658 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Created connection 2011997442.
13:17:28.658 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@77eca502]
13:17:28.660 [main] DEBUG com.mybatis.dao.EmployeeMapper.getEmpById - ==> Preparing: select * from tbl_employee where id = ?
13:17:28.695 [main] DEBUG com.mybatis.dao.EmployeeMapper.getEmpById - ==> Parameters: 101(Integer)
13:17:28.728 [main] DEBUG com.mybatis.dao.EmployeeMapper.getEmpById - <== Total: 1
13:17:28.729 [main] INFO com.mybatis.MybatisTest - 101 jack man
13:17:28.729 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@77eca502]
13:17:28.730 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@77eca502]
13:17:28.730 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 2011997442 to pool.
13:17:28.730 [main] INFO com.mybatis.MybatisTest - 查询完成!
Process finished with exit code 0
13:17:26.491 [main] INFO com.mybatis.MybatisTest - Mapper class: class com.sun.proxy.$Proxy5
通过sqlSession.getMapper(EmployeeMapper.class);是通过代理获取的接口示例