MyBatis框架UserMapperTest类的改进及核心配置文件

人生必须的知识就是引人向光明方面的明灯。这篇文章主要讲述MyBatis框架UserMapperTest类的改进及核心配置文件相关的知识,希望能为你提供帮助。
一、改造UserMapperTest类的代码(将公共代码创建factory和sqlSession,关闭sqlSession写在MyBatisUtil里面)在src下面新建一个包cn.smbms.utils,在新建的包cn.smbms.utils里面新建类MyBatisUtil,将UserMapperTest类里面的创建factory,sqlSession和关闭sqlSession写在MyBatisUtil里面改造后MyBatisUtil里面代码如下:package cn.smbms.utils; import java.io.IOException; import java.io.InputStream; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; 【MyBatis框架UserMapperTest类的改进及核心配置文件】import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class MyBatisUtil {private static SqlSessionFactory factory; static{try {InputStream is = Resources.getResourceAsStream("mybatis-config.xml"); factory = new SqlSessionFactoryBuilder().build(is); } catch (IOException e) {e.printStackTrace(); }}public static SqlSession createSqlSession(){return factory.openSession(false); }public static void closeSqlSession(SqlSession sqlSession){if(null!=sqlSession){sqlSession.close(); }}}改造后UserMapperTest代码如下:package cn.smbms.dao.user; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import cn.smbms.utils.MyBatisUtil; public class UserMapperTest {private Logger logger = Logger.getLogger(UserMapperTest.class); @Testpublic void test() {int count = 0; SqlSession sqlSession = null; try {sqlSession = MyBatisUtil.createSqlSession(); count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count"); logger.debug("UserMapperTest count---> " + count); } catch (Exception e) {e.printStackTrace(); }finally{MyBatisUtil.closeSqlSession(sqlSession); }}}二、如何查看源码:比如查看SqlSession源码按住ctrl健鼠标移动到SqlSession上点击Open declared Type可打开SqlSession类,执行window—show view—outline可以查看 SqlSession的相关方法三、改造UserMapperTest类的代码实现用户列表的查询(使用sqlSession.selectList()方法)在UserMapper.xml文件中增加查询用户列表配置:增加后的完整代码为:< ?xml version="1.0" encoding="UTF-8" ?> < !DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"> < mapper namespace="cn.smbms.dao.user.UserMapper"> < !-- 查询用户表记录数 --> < select id="count" resultType="int"> select count(1) as count from smbms_user< /select> < !-- 查询用户列表 --> < select id="getUserList" resultType="cn.smbms.pojo.User"> select * from smbms_user< /select> < /mapper> 在UserMapperTest类中增加testGetUserList()方法增加后代码:package cn.smbms.dao.user; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import cn.smbms.pojo.User; import cn.smbms.utils.MyBatisUtil; public class UserMapperTest {private Logger logger = Logger.getLogger(UserMapperTest.class); @Testpublic void test() {int count = 0; SqlSession sqlSession = null; try {sqlSession = MyBatisUtil.createSqlSession(); count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count"); logger.debug("UserMapperTest count---> " + count); } catch (Exception e) {e.printStackTrace(); }finally{MyBatisUtil.closeSqlSession(sqlSession); }}@Testpublic void testGetUserList() {List< User> userList=null; SqlSession sqlSession = null; try {sqlSession = MyBatisUtil.createSqlSession(); userList = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserList"); } catch (Exception e) {e.printStackTrace(); }finally{MyBatisUtil.closeSqlSession(sqlSession); }for(User user:userList){logger.debug("UserMapperTest userCode---> " + user.getUserCode()+"and userName:"+user.getUserName()); }}}四、使用sqlSession.getMapper(Mapper.class)方法改造UserMapperTest由于userList = sqlSession.selectList("cn.smbms.dao.user.UserMapper.getUserList"); 中cn.smbms.dao.user.UserMapper.getUserList字符串书写容易出错在cn.smbms.dao.user下创建UserMapper接口接口中代码如下:package cn.smbms.dao.user; import java.util.List; import cn.smbms.pojo.User; public interface UserMapper {public List< User> getUserList(); }UserMapperTest修改后的代码如下:package cn.smbms.dao.user; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.log4j.Logger; import org.junit.Test; import cn.smbms.pojo.User; import cn.smbms.utils.MyBatisUtil; public class UserMapperTest {private Logger logger = Logger.getLogger(UserMapperTest.class); @Testpublic void test() {int count = 0; SqlSession sqlSession = null; try {sqlSession = MyBatisUtil.createSqlSession(); count = sqlSession.selectOne("cn.smbms.dao.user.UserMapper.count"); logger.debug("UserMapperTest count---> " + count); } catch (Exception e) {e.printStackTrace(); }finally{MyBatisUtil.closeSqlSession(sqlSession); }}@Testpublic void testGetUserList() {List< User> userList=null; SqlSession sqlSession = null; try {sqlSession = MyBatisUtil.createSqlSession(); userList = sqlSession.getMapper(UserMapper.class).getUserList(); } catch (Exception e) {e.printStackTrace(); }finally{MyBatisUtil.closeSqlSession(sqlSession); }for(User user:userList){logger.debug("UserMapperTest userCode---> " + user.getUserCode()+"and userName:"+user.getUserName()); }}}五、修改mybatis-config.xml的properties配置将< properties resource="database.properties"/> 修改为:< properties> < property name="driver" value=https://www.songbingjia.com/android/"com.mysql.jdbc.Driver"/> < property name="url" value=https://www.songbingjia.com/android/"jdbc:mysql://127.0.0.1:3306/smbms"/> < property name="user" value=https://www.songbingjia.com/android/"root"/> < property name="password" value=https://www.songbingjia.com/android/"112"/> < /properties> 六、将UserMapper.xml中的resultType="cn.smbms.pojo.User"简写成resultType="User"的配置在mybatis-config.xml里面配置< typeAliases> 代码选中Design在configuration处右键-Add Child-typeAliases,系统会自动的将< typeAliases> 标签插入在指定位置,在标签里面配置如下代码< typeAliases> < typeAlias alias="user" type="cn.smbms.pojo.User" /> < /typeAliases> (这里注意typeAlias标签最好自动生成,不然容易报错)然后将UserMapper.xml中的resultType="cn.smbms.pojo.User"改成resultType="User"如果太多了,配置起来很麻烦,与是进一步做如下改进将mybatis-config.xml里面的< typeAliases> 配置成如下:< typeAliases> < package name="cn.smbms.pojo"/> < /typeAliases> 七、使用URL的方式配置mybatis-config.xml中的mappers标签在工作空间中找到UserMapper.xml,将其复制到D:javastudy练习素材,进入mybatis-config.xml中将< mapper resource="cn/smbms/dao/user/UserMapper.xml"/> 改成< mapper url="file:///D:/javastudy/练习素材/UserMapper.xml"/>

    推荐阅读