MyBatis Dao开发的两种方式
两种dao的实现方式:
- 原始dao的开发方式
- 使用Mapper代理形式开发方式
a)直接配置Mapper代理
b)使用扫描包配置Mapper代理
现在有个需求:
- 实现根据用户id查询
- 实现根据用户名模糊查询
- 添加用户
创建pojo:
public class User { private int id; private String username;// 用户姓名 private String sex;// 性别 private Date birthday;// 生日 private String address;// 地址 get/set。。。 }
传统dao的开发方式
原始的DAO开发接口+实现类来完成。
需要dao实现类需要继承SqlsessionDaoSupport类
1.实现Mapper.xml
编写User.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="test"> <!-- 根据用户id查询 --> <select id="queryUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> <!-- 根据用户名模糊查询用户 --> <select id="queryUserByUsername" parameterType="string" resultType="user"> select * from user where username like '%${value}%' </select> <!-- 添加用户 --> <insert id="saveUser" parameterType="user"> <selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="int"> select last_insert_id() </selectKey> insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address}) </insert> </mapper>
2.加载Mapper.xml
在SqlMapConfig如下图进行配置:
3.实现UserDao接口
public interface UserDao { /** * 根据id查询用户 * * @param id * @return */ User queryUserById(int id); /** * 根据用户名模糊查询用户列表 * * @param username * @return */ List<User> queryUserByUsername(String username); /** * 保存 * * @param user */ void saveUser(User user); }
4.实现UserDaoImpl实现类
编写DAO实现类,实现类必须集成SqlSessionDaoSupport
SqlSessionDaoSupport提供getSqlSession()方法来获取SqlSession
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao { @Override public User queryUserById(int id) { // 获取SqlSession SqlSession sqlSession = super.getSqlSession(); // 使用SqlSession执行操作 User user = sqlSession.selectOne("queryUserById", id); // 不要关闭sqlSession return user; } @Override public List<User> queryUserByUsername(String username) { // 获取SqlSession SqlSession sqlSession = super.getSqlSession(); // 使用SqlSession执行操作 List<User> list = sqlSession.selectList("queryUserByUsername", username); // 不要关闭sqlSession return list; } @Override public void saveUser(User user) { // 获取SqlSession SqlSession sqlSession = super.getSqlSession(); // 使用SqlSession执行操作 sqlSession.insert("saveUser", user); // 不用提交,事务由spring进行管理 // 不要关闭sqlSession } }
5.配置dao
把dao实现类配置到spring容器中,如下图
6.测试方法
编写测试方法如下:
public class UserDaoTest { private ApplicationContext context; @Before public void setUp() throws Exception { this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); } @Test public void testQueryUserById() { // 获取userDao UserDao userDao = this.context.getBean(UserDao.class); User user = userDao.queryUserById(1); System.out.println(user); } @Test public void testQueryUserByUsername() { // 获取userDao UserDao userDao = this.context.getBean(UserDao.class); List<User> list = userDao.queryUserByUsername("张"); for (User user : list) { System.out.println(user); } } @Test public void testSaveUser() { // 获取userDao UserDao userDao = this.context.getBean(UserDao.class); User user = new User(); user.setUsername("曹操"); user.setSex("1"); user.setBirthday(new Date()); user.setAddress("三国"); userDao.saveUser(user); System.out.println(user); } }
Mapper代理形式开发dao
1.实现Mapper.xml
编写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.itcast.mybatis.mapper.UserMapper"> <!-- 根据用户id查询 --> <select id="queryUserById" parameterType="int" resultType="user"> select * from user where id = #{id} </select> <!-- 根据用户名模糊查询用户 --> <select id="queryUserByUsername" parameterType="string" resultType="user"> select * from user where username like '%${value}%' </select> <!-- 添加用户 --> <insert id="saveUser" parameterType="user"> <selectKey keyProperty="id" keyColumn="id" order="AFTER" resultType="int"> select last_insert_id() </selectKey> insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address}) </insert> </mapper>
2.实现UserMapper接口
public interface UserMapper { /** * 根据用户id查询 * * @param id * @return */ User queryUserById(int id); /** * 根据用户名模糊查询用户 * * @param username * @return */ List<User> queryUserByUsername(String username); /** * 添加用户 * * @param user */ void saveUser(User user); }
3.配置mapper代理(方式一)
在applicationContext.xml添加配置
MapperFactoryBean也是属于mybatis-spring整合包
<!-- Mapper代理的方式开发方式一,配置Mapper代理对象 --> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!-- 配置Mapper接口 --> <property name="mapperInterface" value="cn.itcast.mybatis.mapper.UserMapper" /> <!-- 配置sqlSessionFactory --> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
4.测试方法
public class UserMapperTest { private ApplicationContext context; @Before public void setUp() throws Exception { this.context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); } @Test public void testQueryUserById() { // 获取Mapper UserMapper userMapper = this.context.getBean(UserMapper.class); User user = userMapper.queryUserById(1); System.out.println(user); } @Test public void testQueryUserByUsername() { // 获取Mapper UserMapper userMapper = this.context.getBean(UserMapper.class); List<User> list = userMapper.queryUserByUsername("张"); for (User user : list) { System.out.println(user); } } @Test public void testSaveUser() { // 获取Mapper UserMapper userMapper = this.context.getBean(UserMapper.class); User user = new User(); user.setUsername("曹操"); user.setSex("1"); user.setBirthday(new Date()); user.setAddress("三国"); userMapper.saveUser(user); System.out.println(user); } }
5.扫描包形式配置mapper(方式二)
<!-- Mapper代理的方式开发方式二,扫描包方式配置代理 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 配置Mapper接口 --> <property name="basePackage" value="cn.itcast.mybatis.mapper" /> </bean> 每个mapper代理对象的id就是类名,首字母小写