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

Posted pere

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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;

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);

@Test

public 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 mapper

PUBLIC "-//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);

@Test

public 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);

}

}

@Test

public 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);

@Test

public 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);

}

}

@Test

public 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="com.mysql.jdbc.Driver"/>

<property name="url" value="jdbc:mysql://127.0.0.1:3306/smbms"/>

<property name="user" value="root"/>

<property name="password" value="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"/>

以上是关于MyBatis框架UserMapperTest类的改进及核心配置文件的主要内容,如果未能解决你的问题,请参考以下文章

DAO层使用mybatis框架有关实体类的有趣细节

MyBatis框架的基本要素-核心接口和类的作用范围

MyBatis框架基础

MyBatis框架基础

MyBatis框架基础

mybatis框架