使用mybatis开发Dao的原始方法,实现根据用户id查询一个用户信息 、根据用户名称模糊查询用户信息列表 、添加用户信息等功能

1.需求

  将下边的功能实现Dao

    根据用户id查询一个用户信息

    根据用户名称模糊查询用户信息列表

    添加用户信息

2. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类

3.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">
<!-- namespace:命名空间,做Sql隔离 -->
<!-- 在mapper标签中要写很多sql语句。在开发项目的过程中有很多人都会写Sql
语句,在最后整合的时候可能会重复。现在我们使用命名空间开进行隔离,比如zhangsan
写的select * from user,我们可以写为:zhangsan:select * from user来进行标识。 -->
<mapper namespace="test">
    <!-- id:sql语句的唯一标识   test:findUserById就可以唯一标识sql语句
         paremeterType:指定传入的参数类型
         resultSetType:返回值结果类型
         #{}占位符:起到占位的左永刚,如果传入的基本类型{String,long,double,int boolean等},那么
         #{}中的变量名称可以随意写。
    -->
    <select id="findUserById" parameterType="java.lang.Integer" resultType="com.huida.po.User">
        <!-- select语句返回的是user对象,所以resultType中写User类的全路径 -->
        select * from user where id=#{id}
    </select>
    
    <!-- 模糊查询
        返回结果可能为集合;如果返回结果为集合,调用selectList(),并且返回类型配置集合中的泛型。集合中存放的就是User,所以返回类型就是User类型
        ${}拼接符:字符串原样拼接。如果传入的基本类型{String,long,double,int boolean等},那么
        ${}中的变量名必须是value.
    -->
    <select id="findUserByUsername" parameterType="java.lang.String" resultType="com.huida.po.User">
        <!-- 模糊查询的占位符需要进行拼接 -->
        select * from user where username like "%${value}%"
    </select>
    
    <!-- 添加 
        添加操作返回值可有可无
        #{}:如果传入的是po类型,那么#{}中的变量名称必须是po中对应的属性
    -->
    <!-- <select id="insertUser" parameterType="com.huida.po.User">
        insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address})
    </select> -->
    <!-- 自增主键返回 -->
    <insert id="insertUser" parameterType="com.huida.po.User">
        <!-- selectKey将主键返回,需要再返回 -->
        <!-- keyProperty:将返回的主键放入传入参数的id中保存。也就是最后的结果通过id保存起来
            order:当前函数相对于insert语句的执行顺序,在insert前执行的是before,在insert之后执行的是after
            resultType:id的类型
         -->
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            select LAST_INSERT_ID()
        </selectKey>
        insert into user(username,birthday,sex,address) values(#{username},#{birthday},#{sex},#{address});
    </insert>
</mapper>

4.Dao接口

package com.huida.dao;

import java.util.List;

import com.huida.po.User;

public interface UserDao {

    public User findUserById(Integer id);
    public List<User> findUserByUserName(String name);
}

5.Dao接口实现方法

package com.huida.dao;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.huida.po.User;

public class UserDaoImpl implements UserDao {

    //拿到工厂,才能得到Session,才能对sql语句进行处理
    private SqlSessionFactory factory;
    //通过构造方法将工厂传入,也就是注入
    public UserDaoImpl(SqlSessionFactory factory) {
        super();
        this.factory = factory;
    }
    @Override
    public User findUserById(Integer id) {
        //创建session
        //sqlSession是线程不安全的,它的最佳使用是在方法体内
        SqlSession openSession=factory.openSession();
        User user=openSession.selectOne("test.findUserById", id);
        return user;
    }
    //模糊查询
    @Override
    public List<User> findUserByUserName(String name) {
        //每个方法创建一个sqlSession
        SqlSession openSession=factory.openSession();
        List<User> list=openSession.selectList("test.findUserByUsername",name);
        return list;
    }
    

}

6.Dao测试

  创建一个JUnit的测试类,对UserDao进行测试。  

  这里我们使用了一个小技巧,因为没执行一个方法都需要创建工厂,所以我们可以将创建工厂的方法拿出来,放在所有测试方法之前,并在前面加一个@Before的注解,这样就会在测试方法前执行这个方法。不能将建了SqlSession的方法提到前面,因为SqlSession的作用范围应该是在方法内。

package com.huida.test;


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.huida.dao.UserDao;
import com.huida.dao.UserDaoImpl;
import com.huida.po.User;

public class UserDaoTest {

    private SqlSessionFactory factory=null;
    //before的作用:在测试方法前执行这个方法
    @Before
    public void init() throws Exception{
        // 通过流将核心配置文件读取进来
        InputStream inputStream = Resources.getResourceAsStream("config/SqlMapConfig.xml");
        // 通过核心配置文件输入流来创建工厂
        factory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    @Test
    public void testFindById(){
        UserDao userDao=new UserDaoImpl(factory);
        User user=userDao.findUserById(1);
        System.out.println(user);
    }
    
    @Test
    public void testFindByUserName(){
        UserDao userDao=new UserDaoImpl(factory);
        List<User> list=userDao.findUserByUserName("li");
        System.out.println(list);
    }
}

猜你喜欢

转载自www.cnblogs.com/wyhluckdog/p/10151728.html
今日推荐