Mybatis是一个持久层框架,用于简化jdbc的开发,mybatis操作数据库是在java程序当中编写SQL语句,然后将SQL语句发给数据库执行
目录
四、lombok库的使用。尝试将实体类采用注解的方式来实现。
六、对以下案例使用mybatis进行添加、删除、修改、更新的操作。
一、在idea中配置好数据源
1.搭建Springboot工程、准备数据库表、实体类
在mybatis数据库中创建一个user表,存放数据
创建一个pojo,专门用来存放实体类 ,将实体类User中添加Getter and Setter方法和toString方法
2.引入mybatis起步依赖,配置Mybatis
选中mybatis框架依赖,驱动包
配置数据库连接信息(相当于jdbc中connection对象):
3.编写SQL语句
编写SQL语句,定义UserMapper接口,加入一个@Mapper注解,程序运行时框架会自动生成接口的实现类对象(代理对象);
再定义一个接口方法,由于现在idea无法识别"select *from user",只能把它当成一个简单的字符串,这时可以加入@Select,代表进行的是select操作,但是这样会导致如果SQL语句发生错误,而idea无法识别到,此时可以进行配置SQL提示,但是使用之前要建立idea与mybatis数据库的连接,idea才可以找到user表。
package com.itccc.mapper;
import com.itccc.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
public List<User> list();
}
单元测试中代码:
package com.itccc;
import com.itccc.mapper.UserMapper;
import com.itccc.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 SpringbootMybatisQuickstartApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
public void testListUser(){
List<User> userList = userMapper.list();
userList.stream().forEach(user ->
{
System.out.println(user);
});
}
}
运行结果,控制台返回User表中所有信息
二、idea的sql 控制台,可以测试sql语句是否正确。
三、完成添加、删除、修改操作的代码
1.相关代码
UserMapper中增删改相关代码
package com.itccc.mapper;
import com.itccc.pojo.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from user")
public List<User> list();
@Insert("insert into user(name, age, gender, phone) value (#{name},#{age},#{gender},#{phone})")
public void insert(User user);
@Delete("delete from user where id = #{id}")
public void deleteById(int id);
@Update("update user set name=#{name},gender=#{gender},age=#{age},phone=#{phone} where id=#{id}")
public void updateById(User user);
@Select("select * from user where id = #{id}")
public User get(int id);
}
测试类中
package com.itccc;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import com.itccc.mapper.UserMapper;
import com.itccc.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 SpringbootMybatisQuickstartApplicationTests {
@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 testListUser(){
List<User> userList = userMapper.list();
userList.stream().forEach(user ->
{
System.out.println(user);
});
}
@Test
public void insert(){
User user = new User();
user.setName("小蔡");
user.setAge((short)18);
user.setGender((short)2);
user.setPhone("18800000006");
userMapper.insert(user);
listAll();
}
@Test
public void delete(){
userMapper.deleteById(9);
listAll();
}
@Test
public void update(){
User user = userMapper.get(6);
user.setName("小刘");
user.setAge((short)18);
user.setGender((short)2);
user.setPhone("18800000007");
userMapper.updateById(user);
listAll();
}
}
2.运行结果
增加数据
删除数据
修改数据
四、lombok库的使用。尝试将实体类采用注解的方式来实现。
引入lombok依赖,实体类的无参构造、全参构造用以及其他功能用@NoArgsConstructor、@AllArgsConstructor和@Data注解替代
package com.itccc.pojo;
import lombok.*;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {
private Integer id;
private String name;
private short age;
private short gender;
private String phone;
}
五、学习idea的调试技巧,并尝试使用
1.为什么要使用debug
在运行代码之后,可能会出现错误或异常信息,没有得到想要的结果等情况,这时可以通过使用debug来查找问题
2.使用debug步骤
添加断点--启动调试--单步执行--观察变量和执行情况,找到并解决问题
六、对以下案例使用mybatis进行添加、删除、修改、更新的操作。
先创建一个诗人表,里面存放了六个诗人的信息,基于此表进行增删改的操作
实体类Poet
package com.itccc.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Poet {
private Integer id;
private String name;
private String gender;
private String dynasty;
private String title;
private String style;
}
PoetMapper代码
package com.itccc.mapper;
import com.itccc.pojo.Poet;
import com.itccc.pojo.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import java.util.List;
@Mapper
public interface PoetMapper {
@Select("select * from poet")
public List<Poet> list();
@Select("select * from poet where id = #{id}")
public Poet get(int id);
@Select("insert into poet(name, gender, dynasty, title, style) value (#{name},#{gender},#{dynasty},#{title},#{style})")
public void insert1(Poet poet);
@Delete("delete from poet where id=#{id}")
public void delete1(Integer id);
@Update("update poet set name=#{name},gender=#{gender},dynasty=#{dynasty},title=#{title},style=#{style} where id=#{id}")
public void update1(Poet poet);
}
测试类中
package com.itccc;
import com.itccc.mapper.PoetMapper;
import com.itccc.pojo.Poet;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
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 SpringbootMybatisQuickstartApplicationTests {
@Autowired
private PoetMapper poetMapper;
@Test
public void listAll1(){
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.print(poet.getDynasty()+",");
System.out.print(poet.getTitle()+",");
System.out.println(poet.getStyle());
}
}
//增加数据
@Test
public void insert1(){
Poet poet = new Poet();
poet.setName("苏轼");
poet.setGender("男");
poet.setDynasty("北宋");
poet.setTitle("文学家、书画家,诗神");
poet.setStyle("清新豪健的诗风和独特的艺术表现力");
poetMapper.insert1(poet);
listAll1();
}
//删除数据
@Test
public void delete1(){
poetMapper.delete1(7);
listAll1();
}
//修改数据
@Test
public void update1(){
Poet poet = poetMapper.get(6);
poet.setName("苏轼");
poet.setGender("男");
poet.setDynasty("北宋");
poet.setTitle("文学家、书画家,诗神");
poet.setStyle("清新豪健的诗风和独特的艺术表现力");
listAll1();
}
}
运行结果:
增加数据
删除数据
修改数据