mybatis 入门进阶之 mapper
由于上节 《mybatis 入门优化》中的dao实现类耦合了user.xml中的statment的id,例如:src.main.resource.userMapper.findUserByName。
这节采用mapper接口的方式实现dao。
IWebUserDao.java
package com.mozi.dao; import java.util.List; import com.mozi.entity.*; public interface IWebUserDao { /**根据用户名称模糊查询用户信息*/ public List<WebUser> findUserByName(String username); /** 添加用户*/ public void insertUser(WebUser user); /** 根据ID删除用户*/ public void deleteUser(String id); /** 根据ID更新用户*/ public void updateUser(WebUser user); }
sqlmap\userMapper.xml,
注意:
1、mapper配置中的namespace必须与接口去路径一致如:com.mozi.dao.IWebUserDao。
2、mapper的statment的id跟接口中的方法名一致,参数类型一致。
<?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"> <!-- namespace命名空间,作用就是对sql进行分类化的管理,理解为sql隔离 注意:使用mapper代理开发时,namespace有特殊作用 --> <mapper namespace="com.mozi.dao.IWebUserDao"> <resultMap id="BaseResultMap" type="com.mozi.entity.WebUser" > <id column="Name" property="name" jdbcType="VARCHAR" /> <result column="TrueName" property="trueName" jdbcType="VARCHAR" /> <result column="Pass" property="pass" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > Name, TrueName, Pass </sql> <select id="findUserById" parameterType="int" resultType="com.mozi.entity.WebUser" > select * from WebUsers where id=#{id} </select> <select id="findUserByName" parameterType="java.lang.String" resultType="com.mozi.entity.WebUser" > select * from WebUsers where Name LIKE '%${value}%' </select> <select id="findUserByName2" parameterType="java.lang.String" resultMap ="BaseResultMap" > select * from WebUsers where Name LIKE '%${value}%' </select> <!-- 添加用户 --> <insert id="insertUser" parameterType="com.mozi.entity.WebUser" > <!-- 使用mysql的uuid(),实现非自增主键的返回。 执行过程:通过uuid()得到主键,将主键设置到user对象的Id的属性中,其次,在insert执行时,从user对象中取出Id属性值; --> insert into WebUsers (Name,TrueName,Pass) values(#{name},#{trueName},#{pass}) </insert> <!-- 删除用户 --> <delete id="deleteUser" parameterType="java.lang.String"> delete from WebUsers where Name=#{name} </delete> <!-- 更新用户 --> <update id="updateUser" parameterType="com.mozi.entity.WebUser"> update WebUsers set Name=#{name},TrueName=#{trueName},Pass=#{pass} where Name=#{name} </update> </mapper>
WebUserDaoMybatis3.java
package com.mozi.dao.impl; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import com.mozi.dao.IWebUserDao; import com.mozi.entity.WebUser; public class WebUserDaoMybatis3 implements IWebUserDao { private SqlSessionFactory sqlSessionFactory; // 需要向dao实现类中注入SqlSessionFactory // 通过构造方法注入 public WebUserDaoMybatis3(SqlSessionFactory sqlSessionFactory) { this.sqlSessionFactory = sqlSessionFactory; } @Override public List<WebUser> findUserByName(String username) { SqlSession sqlSession = sqlSessionFactory.openSession(); // 创建Usermapper对象,mybatis自动生成mapper代理对象 IWebUserDao mapper = sqlSession.getMapper(IWebUserDao.class); List<WebUser> list = mapper.findUserByName(username); sqlSession.close(); return list; } @Override public void insertUser(WebUser user) { SqlSession sqlSession = sqlSessionFactory.openSession(); IWebUserDao mapper = sqlSession.getMapper(IWebUserDao.class); // 执行插入操作 mapper.insertUser(user); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } @Override public void deleteUser(String id) { SqlSession sqlSession = sqlSessionFactory.openSession(); IWebUserDao mapper = sqlSession.getMapper(IWebUserDao.class); // 执行插入操作 mapper.deleteUser(id); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } @Override public void updateUser(WebUser user) { SqlSession sqlSession = sqlSessionFactory.openSession(); IWebUserDao mapper = sqlSession.getMapper(IWebUserDao.class); // 执行插入操作 mapper.updateUser(user); // 提交事务 sqlSession.commit(); // 释放资源 sqlSession.close(); } }
测试类WebUserDaoImplTest.java
package com.mozi.test.java; import static org.junit.Assert.*; import java.io.IOException; import java.io.InputStream; import java.util.List; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.junit.Before; import org.junit.Test; import com.mozi.dao.IWebUserDao; import com.mozi.dao.impl.WebUserDaoJdbc; import com.mozi.dao.impl.WebUserDaoMybatis; import com.mozi.dao.impl.WebUserDaoMybatis2; import com.mozi.dao.impl.WebUserDaoMybatis3; import com.mozi.entity.WebUser; public class WebUserDaoImplTest { private static SqlSessionFactory sqlSessionFactory = null; private static IWebUserDao dao = null; static{ String resource = "SqlMapConfig.xml"; InputStream inputStream = null; try { inputStream = Resources.getResourceAsStream(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); dao = new WebUserDaoMybatis3(sqlSessionFactory); } @Test public void findUserByName() { List<WebUser> userList = dao.findUserByName("aa"); System.out.println(userList); assertNotNull(userList); } @Test public void insertUser() { WebUser user = new WebUser(); user.setName("5"); user.setTrueName("lisi"); user.setPass("5"); dao.insertUser(user); } @Test public void deleteUser() { dao.deleteUser("5"); } @Test public void updateUser() { WebUser user = new WebUser(); user.setName("5"); user.setTrueName("lisi"); user.setPass("666"); dao.updateUser(user); } }