博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis开发Dao
阅读量:5941 次
发布时间:2019-06-19

本文共 8010 字,大约阅读时间需要 26 分钟。

参考文献:

原始dao方式

需要写dao接口和dao实现类。

  • 创建dao接口:UserDao.java
package cn.itcast.mybatis.dao;import cn.itcast.mybatis.po.Employee;public interface UserDao {    public Employee findUserById(int id) throws Exception;    public void insertUser(Employee employee) throws Exception;    public void deleteUser(int id) throws Exception;}
  • 创建dao实现类:UserDaoImpl.java
package cn.itcast.mybatis.dao;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import cn.itcast.mybatis.po.Employee;public class UserDaoImpl implements UserDao{    private SqlSessionFactory sqlSessionFactory;    public  UserDaoImpl(SqlSessionFactory sqlSessionFactory) {        this.sqlSessionFactory = sqlSessionFactory;    }    @Override    public Employee findUserById(int id) throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        Employee employee = sqlSession.selectOne("test.findUserById",id);        sqlSession.close();        return employee;    }    @Override    public void insertUser(Employee employee) throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        sqlSession.insert("test.insertUser",employee);        sqlSession.commit();        sqlSession.close();     }    @Override    public void deleteUser(int id) throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        sqlSession.delete("test.deleteUser",id);        sqlSession.commit();        sqlSession.close();     }}
  • 测试方法:
package cn.itcast.mybatis.dao;import static org.junit.Assert.*;import java.io.InputStream;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 cn.itcast.mybatis.po.Employee;public class UserDaoImplTest {    private SqlSessionFactory sqlSessionFactory;    @Before    public void setUp() throws Exception{        String resource="SqlMapConfig.xml";        InputStream inputStream= Resources.getResourceAsStream(resource);        sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);    }    @Test    public void test() throws Exception {        UserDao userDao = new UserDaoImpl(sqlSessionFactory);        Employee employee = userDao.findUserById(3);        System.out.println(employee);    }}

缺点:

接口实现类方法中存在大量模板方法,设想能否将这些代码提取出来。

另外调用SqlSession方法时,将Statement的id硬编码了。
调用SqlSession方法时,传入的变量,由于SqlSession方法使用泛型,即使变量类型传入错误,在编译阶段也不报错,不利于开发。

mapper代理方法

  • xml映射文件 (UserMapper.xml)
    ①在mapper.xml中namespace等于mapper接口地址
    ②mapper.java接口中的方法名和mapper.xml中的Statement的id一致
    ③mapper.java接口中的方法输入参数类型和mapper.xml中Statement的parameterType指定类型一致
    ④mapper.java接口中的方法返回值类型和mapper.xml中Statement的resultType指定的类型一致
  • mapper接口,相当于dao接口(UserMapper.java)
package cn.itcast.mybatis.mapper;import cn.itcast.mybatis.po.Employee;public interface UserMapper {    public Employee findUserById(int id) throws Exception;        public java.util.List
findUserByName(String name) throws Exception; public void insertUser(Employee employee ) throws Exception; public void deleteUser(int id) throws Exception;}
  • 还需在SqlMapConfig.xml加载映射文件
  • 测试文件
package cn.itcast.mybatis.mapper;import static org.junit.Assert.*;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;import org.junit.Before;import org.junit.Test;import cn.itcast.mybatis.po.Employee;public class UserMapperTest {private SqlSessionFactory sqlSessionFactory;    @Before    public void setUp() throws Exception{        String resource="SqlMapConfig.xml";        InputStream inputStream= Resources.getResourceAsStream(resource);        sqlSessionFactory =new SqlSessionFactoryBuilder().build(inputStream);    }    @Test    public void test() throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        Employee employee =userMapper.findUserById(2);        System.out.println(employee);    }        @Test    public void test1() throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        java.util.List
list =userMapper.findUserByName("zhao"); sqlSession.close(); System.out.println(list); }}

通过调用单独的文件连接数据库

  • SqlMapConfig.xml
  • db.properties(和SqlMapConfig.xml在同一目录下)
jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8jdbc.username=rootjdbc.password=

pojo包装类型实现

  • UserMapper.xml(定义用户信息综合查询,查询条件复杂)
  • UserQueryVo.java(包装对象)
package cn.itcast.mybatis.po;public class UserQueryVo {    private EmployeeCustom employeeCustom;    public EmployeeCustom getEmployeeCustom() {        return employeeCustom;    }    public void setEmployeeCustom(EmployeeCustom employeeCustom) {        this.employeeCustom = employeeCustom;    }}
  • EmployeeCustom.java
package cn.itcast.mybatis.po;public class EmployeeCustom extends Employee{   }
  • 测试文件
@Test    public void test1() throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);                //创建包装对象,设置查询条件        UserQueryVo userQueryVo= new UserQueryVo();        EmployeeCustom employeeCustom=new EmployeeCustom();        employeeCustom.setSex("nv");        employeeCustom.setUsername("zhou");        userQueryVo.setEmployeeCustom(employeeCustom);               //调用userMapper的方法        java.util.List
list =userMapper.findUserList(userQueryVo); sqlSession.close(); System.out.println(list); }

动态sql判断

  • UserMapper.xml(判断传入的sex和username是否为空,若是空则不进行拼接)
  • 测试文件
@Test    public void test1() throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        UserQueryVo userQueryVo= new UserQueryVo();        EmployeeCustom employeeCustom=new EmployeeCustom();        //employeeCustom.setSex("nv");//注释此句后,查询语句中没有sex这个条件        employeeCustom.setUsername("zhou");        userQueryVo.setEmployeeCustom(employeeCustom);        java.util.List
list =userMapper.findUserList(userQueryVo); sqlSession.close(); System.out.println(list); }

** 进行select * from users where id=1 or id=10 or id=11 ;**

  • 在上面的UserMapper.xml中添加
id=#{user_id}
  • 测试文件
@Test    public void test1() throws Exception {        SqlSession sqlSession =sqlSessionFactory.openSession();        UserMapper userMapper = sqlSession.getMapper(UserMapper.class);        UserQueryVo userQueryVo= new UserQueryVo();        EmployeeCustom employeeCustom=new EmployeeCustom();        //employeeCustom.setSex("nv");        employeeCustom.setUsername("zhou");        java.util.List
ids =new ArrayList
(); ids.add(1); ids.add(10); ids.add(14); userQueryVo.setIds(ids); userQueryVo.setEmployeeCustom(employeeCustom); java.util.List
list =userMapper.findUserList(userQueryVo); sqlSession.close(); System.out.println(list); }

转载地址:http://zvqtx.baihongyu.com/

你可能感兴趣的文章
常用的正则表达式分享
查看>>
我的世界:一个村落(其一)
查看>>
SKChoosePopView 一个HUD风格的可定制化选项弹窗的快速解决方案
查看>>
(二十)java多线程之ScheduledThreadPoolExecutor
查看>>
【译】码农生涯十六条不要
查看>>
sublime快捷键
查看>>
认识jQuery及jQuery选择器
查看>>
从前后端分离到GraphQL,携程如何用Node实现?\n
查看>>
JavaScript标准库系列——RegExp对象(三)
查看>>
Linux Namespace系列(09):利用Namespace创建一个简单可用的容器
查看>>
js深度解析url地址
查看>>
web入门+书籍推荐
查看>>
OS X 下在代码中枚举所有进程的方法
查看>>
eventEmitter3源码分析与学习
查看>>
关于缓存命中率的几个关键问题!
查看>>
Mysql Proxy的安装配置详细教程
查看>>
Python使用MySQL数据库(新)
查看>>
ThinkSNS积分商城系统 一站式解决企业商城建站需求
查看>>
集成七牛云储存
查看>>
记一次windows的安装
查看>>