使用spring boot + mybatis 开发简单的增删改查服务
使用spring boot + mybatis 开发简单的增删改查服务
整体文件目录结构:
pom.xml内容:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.xin</groupId> <artifactId>20220621</artifactId> <version>1.0-SNAPSHOT</version> <parent> <artifactId>spring-boot-starter-parent</artifactId> <groupId>org.springframework.boot</groupId> <version>2.2.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.2.5</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> </dependencies> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> </properties> </project>
application.yml内容:
server: port: 8002 spring: datasource: url: jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false username: demo_user password: demo12356 driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.demo.model
User.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.demo.dao.UserDao"> <resultMap id="BaseResultMap" type="com.demo.model.User"> <id column="userid" jdbcType="INTEGER" property="userid"/> <result column="username" jdbcType="VARCHAR" property="username"/> <result column="password" jdbcType="VARCHAR" property="password"/> </resultMap> <select id="getlist" resultMap="BaseResultMap"> select userid, username,password from userlogin </select> <insert id="add" parameterType="com.demo.model.User"> insert into userlogin(username,password) values(#{username},#{password}) </insert> <update id="update" parameterType="com.demo.model.User"> update userlogin set username = #{username},password = #{password} where userid = #{userid} </update> <select id="queryById" resultMap="BaseResultMap"> select userid, username,password from userlogin where userid = #{userid} </select> </mapper>
主入口启动文件:AppMain.java
package com.demo; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication //@MapperScan("com.demo.dao") public class AppMain { public static void main(String[] args) { SpringApplication.run(AppMain.class, args); } }
接口文件:IUserService
package com.demo.service; import com.demo.model.User; import java.util.List; public interface IUserService { User queryById(Integer id); boolean add(User user); boolean update(User user); List<User> getlist(); }
服务文件:UserService.java
package com.demo.service.impl; import com.demo.dao.UserDao; import com.demo.model.User; import com.demo.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserService implements IUserService { @Autowired private UserDao userDao; public User queryById(Integer id) { return userDao.queryById(id); } public boolean add(User user){ return userDao.add(user); } public boolean update(User user){ return userDao.update(user); } public List<User> getlist(){ return userDao.getlist(); } }
User实体类:User
package com.demo.model; import lombok.Data; import java.util.Date; @Data public class User { private Integer userid; private String password; private String username; }
DAO:UserDao
package com.demo.dao; import com.demo.model.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserDao { User queryById(Integer userid); boolean add(User user); boolean update(User user); List<User> getlist(); }
API接口:UserController
package com.demo.controller; import com.demo.model.User; import com.demo.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping(value="/user") public class UserController { @Autowired private IUserService userService; @GetMapping(value = "/queryById") public User queryById(int userid) { return userService.queryById(userid); } @GetMapping(value="getlist") public List<User> getlist(){ return userService.getlist(); } @PostMapping(value = "/add") public boolean addUser(User user){ return userService.add(user); } @PostMapping(value = "update") public boolean updateUser(User user){ return userService.update(user); } }