添加日志文件log4j.properties 添加log4j日志文件,可以打印mybatis实际执行的sql语句,方便代码调试
新建日志文件log4j.properties
1 2 3 4 5 6 7 8 log4j.rootLogger =DEBUG, stdout log4j.logger.org.mybatis.example.BlogMapper =TRACE log4j.appender.stdout =org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout =org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern =%5p [%t] - %m%n
在mybatis.cfg.xml添加log4j的配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 <?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 > <settings > <setting name ="logImpl" value ="LOG4J" /> </settings > <environments default ="mysql_test" > <environment id ="mysql_test" > <transactionManager type ="JDBC" > </transactionManager > <dataSource type ="POOLED" > <property name ="driver" value ="com.mysql.jdbc.Driver" /> <property name ="url" value ="jdbc:mysql://localhost:3306/mybatis_test" /> <property name ="username" value ="root" /> <property name ="password" value ="123456" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="com/course/mapper/UserMapper.xml" /> </mappers > </configuration >
1 2 3 4 5 6 7 8 9 10 DEBUG [main] - 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. DEBUG [main] - Created connection 2059461664. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7ac0e420] DEBUG [main] - ==> Preparing: select * from user DEBUG [main] - ==> Parameters: DEBUG [main] - <== Total: 10 DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7ac0e420] DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7ac0e420] DEBUG [main] - Returned connection 2059461664 to pool.
添加工具类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.course.util;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil { private static InputStream inputStream = MyBatisUtil.class.getResourceAsStream("/mybatis.cfg.xml" ); private static SqlSessionFactoryBuilder sessionFactoryBuilder = new SqlSessionFactoryBuilder (); private static SqlSessionFactory sqlSessionFactory = sessionFactoryBuilder.build(inputStream); public static SqlSession openSqlSession () { SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } public static void closeSqlSession (SqlSession sqlSession) { sqlSession.commit(); sqlSession.close(); } }
或者
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package com.course.util;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil2 { private static SqlSessionFactory sqlSessionFactory = null ; static { InputStream inputStream = MyBatisUtil2.class.getResourceAsStream("/mybatis.cfg.xml" ); SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder (); sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream); } public static SqlSession openSqlSession () { SqlSession sqlSession = sqlSessionFactory.openSession(); return sqlSession; } public static void closeSqlSession (SqlSession sqlSession) { sqlSession.commit(); sqlSession.close(); } }
接口的实现类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 package com.course.dao.impl;import java.util.List;import org.apache.ibatis.session.SqlSession;import com.course.dao.UserDao;import com.course.util.MyBatisUtil;import com.course.util.MyBatisUtil2;import com.course.vo.User;public class UserDaoImpl implements UserDao { @Override public void addUser (User user) { SqlSession sqlSession = MyBatisUtil.openSqlSession(); sqlSession.insert("com.course.dao.abc.UserDao.addUserXml" , user); MyBatisUtil.closeSqlSession(sqlSession); } @Override public void updateUser (User user) { SqlSession sqlSession = MyBatisUtil.openSqlSession(); sqlSession.update("com.course.dao.abc.UserDao.updateUser" , user); MyBatisUtil.closeSqlSession(sqlSession); } @Override public void deleteUser (Integer id) { SqlSession sqlSession = MyBatisUtil.openSqlSession(); sqlSession.delete("com.course.dao.abc.UserDao.deleteUser" , id); MyBatisUtil.closeSqlSession(sqlSession); } @Override public User getUserById (Integer id) { SqlSession sqlSession = MyBatisUtil.openSqlSession(); User user = sqlSession.selectOne("com.course.dao.abc.UserDao.getUserById" , id); MyBatisUtil.closeSqlSession(sqlSession); return user; } @Override public List<User> getAllUser () { SqlSession sqlSession = MyBatisUtil2.openSqlSession(); List<User> userList = sqlSession.selectList("com.course.dao.abc.UserDao.getAllUser" ); MyBatisUtil2.closeSqlSession(sqlSession); return userList; } }
mybatis.cfg.xml配置文件详解 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 <?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 > <settings > <setting name ="logImpl" value ="LOG4J" /> </settings > <environments default ="mysql_test" > <environment id ="mysql_test" > <transactionManager type ="JDBC" > </transactionManager > <dataSource type ="POOLED" > <property name ="driver" value ="com.mysql.jdbc.Driver" /> <property name ="url" value ="jdbc:mysql://localhost:3306/mybatis_test" /> <property name ="username" value ="root" /> <property name ="password" value ="123456" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="com/course/mapper/UserMapper.xml" /> </mappers > </configuration >
引入db.properties 新建db.properties
1 2 3 4 driver =com.mysql.jdbc.Driver url =jdbc:mysql://localhost:3306/mybatis_test username =root password =123456
修改mybatis.cfg.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 <?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 resource ="db.properties" > </properties > ... </configuration >