之前就学习过mybatis,今天又结合官方文档重新来回顾下。前面都挺顺利的,后面碰到日志打印的问题。搞了好久才实现,百度别人的例子,但不是不完整就是在自己这里没实现,所以把自己这里的详细列出来,供大家参考。
项目结构如下:
![项目结构](https://img-blog.csdn.net/20170527152141423?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWl3dXhpYTIz/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
1,mybatis的主配置文件mybatis.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>
<typeAliases>
<typeAlias alias="sysUser" type="org.mybatis.domin.SysUser"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/>
<property name="username" value="jin1"/>
<property name="password" value="jin123"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
2,然后就是sql映射文件BlogMapper.xml:
<?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="org.mybatis.example.SysUserMapping">
<select id="selectBlog" resultType="hashmap" >
select * from system_user where id = #{id}
</select>
<insert id="insertUser">
insert into system_user (id,username)values(#{id},#{username})
</insert>
</mapper>
实体类我这里就不列出来了。因为mybatis没法在运行时调试查看sql语句,所以通过日志将其sql打印出来还是比较重要的(对于我们初学者而言)。
3,log4j.properties日志文件如下:
log4j.rootLogger=DEBUG,stdout
log4j.logger.org.mybatis=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p %d %c %m%n
4,大家可能看到我的lib包下面有好几个日志的jar,这里是我最开始不知道mybatis用那个jar,所以都拷贝过来。后面查看mybatis的文档,发现是这么说的:
具体选择哪个日志实现工具由MyBatis的内置日志工厂确定。它会使用最先找到的(按上文列举的顺序查找)
这里说明了Mybatis会自己根据优先级去查找,所以都放进去也是没有问题的。
5,接下来就可以写demo类了。
package org.mybatis.demo;
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.mybatis.domin.SysUser;
import java.io.InputStream;
import java.util.Map;
/**
* Created by 74039 on 2017-05-24.
*/
public class Demo1 {
public static void main(String[] args) {
try{
//主配置文件的路径
String resource = "mybatis.xml";
//通过mybatis自定义的Resources加载文件
InputStream inputStream = Resources.getResourceAsStream(resource);
///构建SqlSessionFactory 整个应用中有一个就行拉 不用重复定义或者获取
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//一次sql执行的数据库操作对象
SqlSession session = sqlSessionFactory.openSession();
String name="org.mybatis.example.SysUserMapping.insertUser";
session.insert(name,new SysUser(23,"jinzi"));
//如果是insert delete update一定要记得提交
session.commit();
System.out.println("over=======");
}catch (Exception e){
e.printStackTrace();
}
}
}
运行demo类,我们就可以在控制台看到如下输出:
DEBUG 2017-05-27 15:10:39,061 org.apache.ibatis.logging.LogFactory Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG 2017-05-27 15:10:39,178 org.apache.ibatis.datasource.pooled.PooledDataSource PooledDataSource forcefully closed/removed all connections.
DEBUG 2017-05-27 15:10:39,178 org.apache.ibatis.datasource.pooled.PooledDataSource PooledDataSource forcefully closed/removed all connections.
DEBUG 2017-05-27 15:10:39,178 org.apache.ibatis.datasource.pooled.PooledDataSource PooledDataSource forcefully closed/removed all connections.
DEBUG 2017-05-27 15:10:39,178 org.apache.ibatis.datasource.pooled.PooledDataSource PooledDataSource forcefully closed/removed all connections.
DEBUG 2017-05-27 15:10:39,260 org.apache.ibatis.transaction.jdbc.JdbcTransaction Opening JDBC Connection
DEBUG 2017-05-27 15:10:39,451 org.apache.ibatis.datasource.pooled.PooledDataSource Created connection 1309331873.
DEBUG 2017-05-27 15:10:39,452 org.apache.ibatis.transaction.jdbc.JdbcTransaction Setting autocommit to false on JDBC Connection [oracle.jdbc.driver.OracleConnection@4e0ad1a1]
DEBUG 2017-05-27 15:10:39,453 org.mybatis.example.SysUserMapping.insertUser ==> Preparing: insert into system_user (id,username)values(?,?)
DEBUG 2017-05-27 15:10:39,502 org.mybatis.example.SysUserMapping.insertUser ==> Parameters: 23(Integer), jinzi(String)
DEBUG 2017-05-27 15:10:39,504 org.mybatis.example.SysUserMapping.insertUser <== Updates: 1
DEBUG 2017-05-27 15:10:39,504 org.apache.ibatis.transaction.jdbc.JdbcTransaction Committing JDBC Connection [oracle.jdbc.driver.OracleConnection@4e0ad1a1]
over=======
通过日志我们可以清晰地看到程序的jdbc内部操作流程。