package com.fairy.mybatisdemo2.entity; import java.io.Serializable; /** * 用户实体类 */ public class User implements Serializable { // 字段 private int uid; private String uname; private String usex; // 访问器 public int getUid() { return uid; } public void setUid(int uid) { this.uid = uid; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public String getSex() { return usex; } public void setSex(String sex) { this.usex = sex; } // 构造器 public User() { } public User(int uid, String uname, String sex) { this.uid = uid; this.uname = uname; this.usex = sex; } // 重写toSering方法 @Override public String toString() { return "User{" + "uid=" + uid + ", uname='" + uname + '\'' + ", usex='" + usex + '\'' + '}'; } }实体类代码
二、定义一个工具类,方便复用,XML映射器UserDao.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="com.fairy.mybatisdemo2.dao.UserDao"> <!-- 根据id查询某一条数据 --> <select id="findById" resultType="User"> select uid, uname, usex from userinfo where uid=#{uid} </select> <!-- 查询所有数据 --> <select id="findAll" resultType="User"> select uid, uname, usex from userinfo </select> <!-- 模糊查询 --> <select id="likeName" resultType="User"> select uid, uname, usex from userinfo where uname like '%${uname}%' </select> <!-- 添加数据 --> <insert id="addUser"> INSERT into userinfo(uid,uname,usex) values(#{uid},#{uname},#{usex}) </insert> <!-- 修改数据 --> <update id="updateUser"> update userinfo set uname=#{uname} , usex=#{usex} where uid = #{uid} </update> <!-- 根据id删除数据 --> <delete id="deleteUser"> delete from userinfo where uid= #{uid} </delete> </mapper>工具类代码
package com.fairy.mybatisdemo2.dao; import com.fairy.mybatisdemo2.entity.User; import java.util.List; /** * 数据访问接口 */ public interface UserDao { /** 根据id查询一条数据 */ User findById(int id); /** 查询全部数据 */ List<User> findAll(); /** 模糊查询 */ List<User> likeName(String name); /** 添加数据 */ int addUser(User user); /** 修改数据 */ int updateUser(User user); /** 删除数据 */ int deleteUser(int uid); }dao包代码
package com.fairy.mybatisdemo2; import com.fairy.mybatisdemo2.dao.UserDao; import com.fairy.mybatisdemo2.entity.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import java.util.List; @SpringBootTest class Mybatisdemo2ApplicationTests { @Autowired UserDao userDao; /** * 根据id查询某个数据 */ @Test public void findByIdTest(){ System.out.println(userDao.findById(6)); } /** * 查询所有的数据 */ @Test public void findAllTest(){ System.out.println(userDao.findAll()); } /*** * 模糊查询 */ @Test public void likeNameTest(){ System.out.println(userDao.likeName("tao")); } /** * 添加数据 */ @Test public void addUserTest(){ User user = new User(); user.setUid(9); user.setUname("淘淘"); user.setSex("男"); System.out.println(userDao.addUser(user)); } /** * 修改数据 */ @Test public void updateUserTest(){ User user; // 首先查询id出来 user = userDao.findById(7); // 然后修改 user.setUname("桃桃"); user.setSex("男"); // 执行方法 System.out.println(userDao.updateUser(user)); } /** * 删除数据 */ @Test public void deleteUpdateTest(){ System.out.println(userDao.deleteUser(5)); } }测试类代码