1. Introduction to the environment
eclipse. InteIIij idea users can also refer to it, simple and clear.
springboot 2.1.8
mybatis persistence layer
mysql database
2. New springboot project
The dependencies in the picture are
springboot DevTools hot deployment depend on
MySQL Driver mysql depend on
mybatis mybatis persistence layer framework
springweb spring reactive web web access dependency
3. Building a package, using mvc mode
The package below tgc.edu.zz will not be explained,
where the mapper package is installed mybatis.xml to write SQL statements
4. Create a new userEntityMapper.xml file in mapper, write the basic SQL statement, and map his corresponding entity class
<?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="tgc.edu.zz.dao.UserDao">
<!-- 查询所有 -->
<select id="findAll" resultType="tgc.edu.zz.entity.UserEntity">
select * from user_entity
</select>
<!--根据id查询-->
<select id="findById" resultType="tgc.edu.zz.entity.UserEntity" parameterType="java.lang.Integer">
select * from user_entity where id = #{id}
</select>
<!--按照姓名模糊查询-->
<select id="findByName" resultType="tgc.edu.zz.entity.UserEntity" parameterType="java.lang.String">
select * from user_entity where name like '%${value}%'
</select>
<!-- 添加 -->
<insert id="insert" parameterType="tgc.edu.zz.entity.UserEntity">
insert into user_entity values(null,#{username},#{password},#{name})
</insert>
<!--修改-->
<update id="update" parameterType="tgc.edu.zz.entity.UserEntity">
update user_entity set username= #{username},password=#{password},name=#{name} where id = #{id}
</update>
<!--删除-->
<delete id="delete" parameterType="java.lang.Integer">
delete from user_entity where id = #{id}
</delete>
</mapper>
5. Create a new DaoUtils.java interface tool class, encapsulate the basic addition, deletion, modification and inspection
package tgc.edu.zz.custom;
import java.util.List;
public interface DaoUtils<E,ID> {
//查询所有
public List<E> findAll();
//根据id删除
public void delete(ID id);
//新增
public void insert(E e);
//修改
public void update (E e);
//根据id查询
public E findById(ID id);
}
6. Create a new ServiceUtils.java tool class and encapsulate the basic addition, deletion and modification of the service layer
package tgc.edu.zz.custom;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
public class ServiceUtils<E,ID> {
@Autowired
private DaoUtils<E, ID> dao;
//查询所有
public List<E> findAll() {
return dao.findAll();
}
//根据id删除
public void delete(ID id) {
dao.delete(id);
}
//新增
public void insert(E e) {
dao.insert(e);
}
//修改
public void update(E e) {
dao.update(e);
}
//根据id查询
public E findById(ID id) {
return dao.findById(id);
}
}
7.entity entity class, I omitted. Build entity model entity class
Create a new UserDao.java interface persistence layer, you only need to inherit DaoUtils.java, there is a basic method of addition, deletion and modification, as shown in the figure:
8. New UserService.java, inherit ServiceUtils.java, there is a basic method of addition, deletion and modification, as shown in the figure:
9. Write control class controller, direct systemout output test
package tgc.edu.zz.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import tgc.edu.zz.entity.UserEntity;
import tgc.edu.zz.service.UserService;
@Controller
@RequestMapping("/main")
public class UserController {
@Autowired
private UserService service;
//根据id为3的查询结果对象
@RequestMapping("/findById")
public void findById() {
UserEntity model = service.findById(3);
System.out.println("用户名:"+model.getUsername()+",密码:"+model.getPassword()+",姓名"+model.getName());
}
//查询所有输出一下显示
@RequestMapping("/findAll")
public List<UserEntity> findAll(){
List<UserEntity> list = service.findAll();
//for循环遍历list结果集合,输出显示一下是否成功
for (UserEntity userEntity : list) {
System.out.println("用户名:"+model.getUsername()+",密码:"+model.getPassword()+",姓名"+model.getName());
}
return list;
}
//删除测试,看数据库id为1的是否删除即可
@RequestMapping("/delete")
public void delete() {
service.delete(1);
}
//新增操作,看数据库是否新增一条数据即可
@RequestMapping("/insert")
public void insert() {
UserEntity model = new UserEntity();
model.setUsername("0012");
model.setPassword("2323");
model.setName("李三");
service.insert(model);
}
//看数据库id为3的是否改了即可
@RequestMapping("/update")
public void update() {
UserEntity model = service.findById(3);
model.setName("李四");
service.update(model);
}
}
10. You must still remember the application.properties configuration file, and then start the configuration, the code is as follows:
spring.datasource.url=jdbc:mysql://localhost:3306/test?serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
server.port=80
server.servlet.context-path=/mybatis
spring.jmx.enabled=false
mybatis.mapperLocations=classpath:mapper/*.xml
11. Do n’t forget one more thing, it ’s very important, it must be added, that is, you need to load the persistence layer file first before running the program.
The following picture shows the loading of the persistence layer package, one annotation and one path.
12. In the end, a simple springboot + mybatis + mysql project is like this.
13. Run the test
(1) New test
adds a new data to the database
(2). Modify the test The
database is successfully modified
(3) Query the data object with id 3
The results of the console output are as follows
(4) All the
console results are as follows
(5) Finally, the deletion of id 3 was
also successfully deleted.
A very simple mybatis + springboot integration to achieve basic additions, deletions, and changes. Thank you for reading the blog. The IT industry can continue to communicate. WeChat: zzaizhangzeng
This blog has private copyright, please indicate it, thank you!