MyBatis:持久层框架,支持自定义SQL、存储过程以及高级映射。免除JDBC代码以及设置参数和获取结果集的工作。通过简单的XML或注解来配置和映射原始类型、接口和java POJO为数据库中记录。(dao层)
1、在idea中配置好数据源
2.创建user类
(第一次输出只有数据地址,原因:没有使用toString方法)
package com.example.demo.pojo;
public class User {
private Integer id; //id(主键)
private String name; //姓名
private Short age; //年龄
private Short gender; //性别
private String phone; //手机号
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
public User(Integer id, String name, Short age, Short gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Short getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3.配置UserMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface UserMapper {
//查询全部信息
@Select("select id, name, age, gender, phone from user")
public List<User> list();
}
4.查询操作
全部查询
①配置UserMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface UserMapper {
//查询全部信息
@Select("select id, name, age, gender, phone from user")
public List<User> list();
}
②Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.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 Demo4ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void listAll(){
List<User> userList = userMapper.list();
for (User user : userList) {
System.out.print(user.getId()+",");
System.out.print(user.getName()+",");
System.out.print(user.getAge()+",");
System.out.print(user.getGender()+",");
System.out.println(user.getPhone());
}
}
}
③结果
部分查询
①配置UserMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface UserMapper {
//查询全部信息
@Select("select id, name, age, gender, phone from user")
public List<User> list();
@Select("select * from user where id= #{id} ")
public User get(int id);
}
②Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.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 Demo4ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void listAll(){
List<User> userList = userMapper.list();
for (User user : userList) {
System.out.print(user.getId()+",");
System.out.print(user.getName()+",");
System.out.print(user.getAge()+",");
System.out.print(user.getGender()+",");
System.out.println(user.getPhone());
}
}
@Test
public void get() {
User c= userMapper.get(5);
System.out.print(c.getId()+",");
System.out.print(c.getName()+",");
System.out.print(c.getAge()+",");
System.out.print(c.getGender()+",");
System.out.println(c.getPhone());
}
}
③结果
5.插入操作
①配置UserMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface UserMapper {
//查询全部信息
@Select("select id, name, age, gender, phone from user")
public List<User> list();
@Insert("insert into user(name, age, gender, phone) values (#{name}, #{age}, #{gender}, #{phone})")
public void insert(User user);
}
②Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.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 Demo4ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void listAll(){
List<User> userList = userMapper.list();
for (User user : userList) {
System.out.print(user.getId()+",");
System.out.print(user.getName()+",");
System.out.print(user.getAge()+",");
System.out.print(user.getGender()+",");
System.out.println(user.getPhone());
}
}
@Test
public void insert() {
// User c =new User(20,"qqq",(short)55,(short)2,"123334");
User c = new User();
c.setName("王维");
c.setGender((short)1);
c.setAge((short)1);
c.setPhone("12343234543");
userMapper.insert(c);
listAll();
}
}
③结果
6.删除操作
①配置UserMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface UserMapper {
//查询全部信息
@Select("select id, name, age, gender, phone from user")
public List<User> list();
@Delete(" delete from user where id=8")
public void delete(int id);
}
②Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.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 Demo4ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void listAll(){
List<User> userList = userMapper.list();
for (User user : userList) {
System.out.print(user.getId()+",");
System.out.print(user.getName()+",");
System.out.print(user.getAge()+",");
System.out.print(user.getGender()+",");
System.out.println(user.getPhone());
}
}
@Test
public void delete() {
userMapper.delete(8);
listAll();
}
}
③结果
7.修改操作
将序号为6的名字改为wangwei
①配置UserMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface UserMapper {
//查询全部信息
@Select("select id, name, age, gender, phone from user")
public List<User> list();
@Select("select * from user where id= #{id} ")
public User get(int id);
@Update("update user set name=#{name} where id=#{id} ")
public int update(User user);
}
②Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.UserMapper;
import com.example.demo.pojo.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 Demo4ApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void listAll(){
List<User> userList = userMapper.list();
for (User user : userList) {
System.out.print(user.getId()+",");
System.out.print(user.getName()+",");
System.out.print(user.getAge()+",");
System.out.print(user.getGender()+",");
System.out.println(user.getPhone());
}
}
@Test
public void update() {
User c= userMapper.get(6);
c.setName("wangwei");
userMapper.update(c);
listAll();
}
@Test
public void get() {
User c= userMapper.get(6);
System.out.print(c.getId()+",");
System.out.print(c.getName()+",");
System.out.print(c.getAge()+",");
System.out.print(c.getGender()+",");
System.out.println(c.getPhone());
}
}
③结果
8.lombok库的使用(提高效率)
现在pom.xml中添加依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
<scope>provided</scope>
</dependency>
前
package com.example.demo.pojo;
public class User {
private Integer id; //id(主键)
private String name; //姓名
private Short age; //年龄
private Short gender; //性别
private String phone; //手机号
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender=" + gender +
", phone='" + phone + '\'' +
'}';
}
public User(Integer id, String name, Short age, Short gender, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.gender = gender;
this.phone = phone;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Short getAge() {
return age;
}
public void setAge(Short age) {
this.age = age;
}
public Short getGender() {
return gender;
}
public void setGender(Short gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
后
package com.example.demo.pojo;
import lombok.*;
@Data//(getter,setter,toString,EqualsAndHashCode)
@AllArgsConstructor//全参构造
@NoArgsConstructor//无参构造
public class User {
private Integer id; //id(主键)
private String name; //姓名
private Short age; //年龄
private Short gender; //性别
private String phone; //手机号
}
结果
9.对以下案例使用mybatis进行添加、删除、修改、更新的操作
①创建表格poet
②创建Poet类,声明变量和方法
package com.example.demo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data//(getter,setter,toString,EqualsAndHashCode)
@AllArgsConstructor//全参构造
@NoArgsConstructor//无参构造
public class Poet {
private Integer id; //id(主键)
private String name; //姓名
private Short gender;
private Short dynasty;
private String title;
private String style;
}
③查询
a.配置PoetMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.Poet;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface PoetMapper {
//查询全部信息
@Select("select * from poet")
public List<Poet> list();
}
b.Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.PoetMapper;
import com.example.demo.pojo.Poet;
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 Demo4ApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void testListPoet(){
List<Poet> poetList=poetMapper.list();
poetList.stream().forEach(poet -> {
System.out.println(poet);
});
}
}
c.结果
④插入
a.配置PoetMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.Poet;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface PoetMapper {
//查询全部信息
@Select("select * from poet")
public List<Poet> list();
@Insert("insert into poet(name, gender, dynasty,title,style) values (#{name}, #{gender}, #{ dynasty},#{title},#{style})")
public void insert(Poet poet);
}
b.Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.PoetMapper;
import com.example.demo.pojo.Poet;
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 Demo4ApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void listAll() {
List<Poet> poetList = poetMapper.list();
for (Poet poet : poetList) {
System.out.print(poet.getId() + ",");
System.out.print(poet.getName() + ",");
System.out.print(poet.getGender() + ",");
System.out.println(poet.getDynasty() + ",");
System.out.println(poet.getTitle() + ",");
System.out.println(poet.getStyle());
}
}
@Test
public void insert() {
Poet c = new Poet();
c.setName("白居易");
c.setGender((short)1);
c.setDynasty("唐代");
c.setTitle("诗魔");
c.setStyle("乐观豁达");
poetMapper.insert(c);
listAll();
}
}
c.结果
⑤删除
a.配置PoetMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.Poet;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface PoetMapper {
//查询全部信息
@Select("select * from poet")
public List<Poet> list();
@Delete(" delete from poet where id= #{id} ")
public void delete(int id);
}
b.Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.PoetMapper;
import com.example.demo.pojo.Poet;
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 Demo4ApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void listAll() {
List<Poet> poetList = poetMapper.list();
for (Poet poet : poetList) {
System.out.print(poet.getId() + ",");
System.out.print(poet.getName() + ",");
System.out.print(poet.getGender() + ",");
System.out.println(poet.getDynasty() + ",");
System.out.println(poet.getTitle() + ",");
System.out.println(poet.getStyle());
}
}
@Test
public void delete() {
poetMapper.delete(8);
listAll();
}
}
c.结果
⑥修改
a.配置PoetMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.Poet;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface PoetMapper {
//查询全部信息
@Select("select * from poet")
public List<Poet> list();
@Select("select * from poet where id= #{id} ")
public Poet get(int id);
@Update("update poet set name=#{name} where id=#{id} ")
public int update(Poet poet);
}
b.Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.PoetMapper;
import com.example.demo.pojo.Poet;
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 Demo4ApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void listAll() {
List<Poet> poetList = poetMapper.list();
for (Poet poet : poetList) {
System.out.print(poet.getId() + ",");
System.out.print(poet.getName() + ",");
System.out.print(poet.getGender() + ",");
System.out.println(poet.getDynasty() + ",");
System.out.println(poet.getTitle() + ",");
System.out.println(poet.getStyle());
}
}
@Test
public void get() {
Poet c= poetMapper.get(3);
System.out.print(c.getId() + ",");
System.out.print(c.getName() + ",");
System.out.print(c.getGender() + ",");
System.out.println(c.getDynasty() + ",");
System.out.println(c.getTitle() + ",");
System.out.println(c.getStyle());
}
@Test
public void update() {
Poet c= poetMapper.get(3);
c.setName("libai");
poetMapper.update(c);
listAll();
}
}
c.结果
⑦查询
a.配置PoetMapper接口
package com.example.demo.mapper;
import com.example.demo.pojo.Poet;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper//在运行时,会自动生成该接口的实现类对象(代理对象),并将对象交给IOC容器管理
public interface PoetMapper {
//查询全部信息
@Select("select * from poet")
public List<Poet> list();
@Select("select * from poet where id= #{id} ")
public Poet get(int id);
}
b.Demo4ApplicationTests测试
package com.example.demo;
import com.example.demo.mapper.PoetMapper;
import com.example.demo.pojo.Poet;
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 Demo4ApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void listAll() {
List<Poet> poetList = poetMapper.list();
for (Poet poet : poetList) {
System.out.print(poet.getId() + ",");
System.out.print(poet.getName() + ",");
System.out.print(poet.getGender() + ",");
System.out.println(poet.getDynasty() + ",");
System.out.println(poet.getTitle() + ",");
System.out.println(poet.getStyle());
}
}
@Test
public void get() {
Poet c= poetMapper.get(5);
System.out.print(c.getId() + ",");
System.out.print(c.getName() + ",");
System.out.print(c.getGender() + ",");
System.out.println(c.getDynasty() + ",");
System.out.println(c.getTitle() + ",");
System.out.println(c.getStyle());
}
}
c.结果