Mybatis 的CRUD

卫寒
2023-12-01

1.创建实体类User

public class User {
    private Integer id;
    private String username;
    private String sex;
    private Date brithday;
    private String address;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Date getBrithday() {
        return brithday;
    }

    public void setBrithday(Date brithday) {
        this.brithday = brithday;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", sex='" + sex + '\'' +
                ", brithday=" + brithday +
                ", address='" + address + '\'' +
                '}';
    }
}

2.定义接口UserDao和CRUD方法

public interface UserDao {
	//全部查询
    List<User> findAll();
	//保存
    void saveUser(User user);
	//更新
    void updateUser(User user);
	//删除
    void deleteUser(Integer id);
	//id查询一个
    User findById(Integer id);
	//模糊查询
    List<User> findByname(String username);
	//查询总记录
    int findTotal();
}

3. 配置映射文件UserDAO.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.dao.UserDao">
    <select id="findAll" resultType="com.mybatis.beans.User">
        SELECT * FROM USER;
    </select>
    <insert id="saveUser" parameterType="com.mybatis.beans.User">
        INSERT INTO USER(username,brithday,sex,address)VALUES (#{username},#{brithday},#{sex},#{address});
    </insert>
    <update id="updateUser" parameterType="com.mybatis.beans.User">
        UPDATE  USER SET  username=#{username},brithday=#{brithday},sex=#{sex},address=#{address} WHERE id=#{id};
    </update>
    <delete id="deleteUser" parameterType="Integer">
        DELETE FROM  USER WHERE id=#{uid};
    </delete>
    <select id="findById" parameterType="Integer" resultType="com.mybatis.beans.User">
        SELECT * FROM  USER WHERE id=#{uid};
    </select>
    <select id="findByname" parameterType="String" resultType="com.mybatis.beans.User">
        SELECT  *FROM  USER WHERE username LIKE #{name};
    </select>
    <select id="findTotal" resultType="int">
      SELECT count(id) FROM USER ;
    </select>

</mapper>

4.测试MybatisCRUD

public class MybatisTest {
   private InputStream in ;
   private SqlSession session;
   private UserDao userDao;
   @Before
   public  void init() throws Exception {
       in = Resources.getResourceAsStream("SqlMapConfig.xml");
      SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
      session = factory.openSession();
      userDao = session.getMapper(UserDao.class);
   }
   @After
   public void destroy()throws Exception{
      session.commit();
      session.close();
      in.close();
   }
   @Test
    public void testfindAll()throws Exception{
       List<User> users = userDao.findAll();
       for (User user:users) {
           System.out.println(user);
       }
   }
   @Test
   public void testSaveUser(){
      User user = new User();
      user.setAddress("胡安娜");
      user.setSex("女");
      user.setUsername("西亚");
      user.setBrithday(new Date());
      userDao.saveUser(user);
   }
   @Test
   public void testUpdate(){
      User user = new User();
      user.setId(7);
      user.setUsername("跟新");
      user.setSex("男");
      user.setAddress("麻阳");
      user.setBrithday(new Date());
      userDao.updateUser(user);
   }
   @Test
   public void testDeleteUser(){
      userDao.deleteUser(7);
   }
   @Test
   public void testFindById(){
      User user = userDao.findById(1);
      System.out.println(user);
   }
   @Test
   public void testFindByname(){
      List<User> users = userDao.findByname("%王%");
      for (User user:users
           ) {
         System.out.println(user);
      }
   }
   @Test
   public void testFindTotal(){
      int cout = userDao.findTotal();
      System.out.println(cout);

   }


}
 类似资料: