SSM框架之Mybatis(3)dao层开发

Mybatis(3)dao层开发

以实现类完成CRUD操作

1、持久层dao层接口的书写

src\main\java\dao\IUserDao.java

package dao;

import domain.QueryVo;
import domain.User;

import java.util.List;

public interface IUserDao {
    /**
     * 查询所有数据
     * @return
     */
    List<User> findAll();
    //List<User> findAll();

    /**
     * 添加用户
     * @param user
     */
    void addUser(User user);

    /**
     * 更新用户
     * @param user
     */
    void updateUser(User user);

    /**
     * 删除用户
     * @param id
     */
    void deleteUser(int id);

    /**
     * 根据id查询用户
     * @param id
     */
    User findById(int id);

    /**
     * 模糊查询用户信息
     * @return
     */
    List<User> findByName(String str);

    /**
     * 使用聚合函数查询
     */
    int findTotal();
}

2、持久层实现类的书写

src\main\java\dao\impl\UserDaoImpl.java

package dao.impl;

import dao.IUserDao;
import domain.QueryVo;
import domain.User;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UserDaoImpl implements IUserDao {
    private SqlSessionFactory factory;

    public UserDaoImpl() {
    }

    public UserDaoImpl(SqlSessionFactory factory) {
        this.factory = factory;
    }

    public List<User> findAll() {
        SqlSession session = factory.openSession();
        List<User> list = session.selectList("dao.IUserDao.findAll");
        session.close();
        return list;
    }

    public void addUser(User user) {
        SqlSession session = factory.openSession();
        int i = session.insert("dao.IUserDao.addUser", user);
        session.commit();
        System.out.println("这是i的值========="+i);
        session.close();


    }

    public void updateUser(User user) {
        SqlSession session = factory.openSession();
        int update = session.update("dao.IUserDao.updateUser", user);
        session.commit();
        session.close();

    }

    public void deleteUser(int id) {
        SqlSession session = factory.openSession();
        int delete = session.delete("dao.IUserDao.deleteUser", id);
        session.commit();
        session.close();
    }

    public User findById(int id) {
        SqlSession session = factory.openSession();
        User list = session.selectOne("dao.IUserDao.findById",id);
        session.close();
        return list;
    }

    public List<User> findByName(String str) {
        SqlSession session = factory.openSession();
        List<User> list = session.selectList("dao.IUserDao.findByName", str);
        session.close();
        return list;
    }

    public List<User> findByVo(QueryVo vo) {
        return null;
    }

    public int findTotal() {
        SqlSession session = factory.openSession();
        int o = session.selectOne("dao.IUserDao.findTotal");
        return o;
    }
}

3、持久层配置文件的书写

src\main\resources\dao\IUserDao.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="dao.IUserDao">

    <select id="findAll" resultType="domain.User">
        select * from user
    </select>

    <!--<select id="findAll" resultType="domain.User">
        select * from user
    </select>-->
    <insert id="addUser" parameterType="domain.User">
      <!--获取插入用户的id-->
        <!--会把id存入传入的user对象中-->
        <selectKey keyProperty="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into user (username,address,sex,birthday) value (#{username},#{address},#{sex},#{birthday})
    </insert>

    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>

    <update id="updateUser" parameterType="domain.User">
        update user set username=#{username},address=#{address},sex=#{sex},birthday=#{birthday}
    </update>

    <select id="findById" parameterType="int" resultType="domain.User">
        select * from user where id=#{id}
    </select>

    <select id="findByName" resultType="domain.User" parameterType="String">
        <!--select * from user where username like '%${value}%'-->
        <!-- 使用上面方式模糊查询时Statment对象通过字符串拼接的方式连接sql语句
          下面的sql语句使用PrepatedStatement的参数占位符有预处理,
          所以下面这种方式使用的多
          -->
        select * from user where username like #{str}
    </select>
    
    <select id="findTotal" resultType="int">
        select count(id) from user
    </select>

</mapper>

4、测试类的书写

src\test\java\test\MybatisTest.java

package test;

import dao.IUserDao;
import dao.impl.UserDaoImpl;
import domain.QueryVo;
import domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.scripting.xmltags.TextSqlNode;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class MybatisTest {
    /**
     * 测试mybatis的CRUD操作
     */
    InputStream in;

    IUserDao uesrdao;
    @Before
    public void init() throws Exception {
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(in);
        uesrdao = new UserDaoImpl(build);

    }

    @After
    public void destory() throws Exception {
        in.close();
    }
    @Test
    public void testFainAll(){
        //查询所有
        List<User> users = uesrdao.findAll();
        for (User usr: users
             ) {
            System.out.println(usr);
        }
    }
   @Test
    public void testAddUser(){
        //添加用户
        User user = new User();
        user.setUsername("wf");
        user.setAddress("中国");
        user.setSex("男");
        user.setBirthday(new Date());

        System.out.println(user);

        uesrdao.addUser(user);

        System.out.println(user);

    }

     @Test
    public void testUpdateUser(){
        //更新用户
        User user = new User();
        user.setUsername("gx");
        user.setAddress("中国");
        user.setSex("女");
        user.setBirthday(new Date());

        uesrdao.updateUser(user);

    }
    @Test
    public void testDeleteUser(){
        //删除用户
        uesrdao.deleteUser(51);

    }

   @Test
    public void testFindOne(){
        //通过id查找用户
        User user = uesrdao.findById(48);
        System.out.println(user);
    }
    @Test
    public void testFindByName(){
        //通过模糊查询查找用户
        List<User> users = uesrdao.findByName("%g%");
        //List<User> users = uesrdao.findByName("g");

        for(User user:users){
            System.out.println(user);

        }
    }

    @Test
    public void testFindTotal(){
        //通过聚合函数查询
        int a = uesrdao.findTotal();
        System.out.println(a);
    }

}

posted @ 2019-04-20 22:59  紫月冰凌  阅读(1202)  评论(0编辑  收藏  举报