idea+SpringBoot整合Mybatis完成增删改查功能
参考地址:https://blog.****.net/baidu_36216018/article/details/79466935
1.idea创建spring boot项目
2.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>com.example</groupId> <artifactId>shujuku</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>shujuku</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <!-- web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <!-- mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
3.项目目录结构:
4.表:表名:user
自己创建一个数据表 字段为id , name, password ,number 四个字段 id为int类型,其他的都为String类型。
5.在src/main/java目录下添加controller层,entity层,mapper层,service层
entity层的实体类User:
package com.example.shujuku.entity; public class User { private int id; private String name; private String password; private String number; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", password=" + password + ", number=" + number + "]"; } }
mapper层的UserMapper类:
package com.example.shujuku.mapper; import java.util.List; import com.example.shujuku.entity.User; import org.apache.ibatis.annotations.Mapper; @Mapper public interface UserMapper { List<User> findUserByName(String name); public List<User> ListUser(); public int insertUser(User user); public int delete(int id); public int Update(User user); }
service层的实现类Userservice:
package com.example.shujuku.service; import java.util.List; import com.example.shujuku.entity.User; import com.example.shujuku.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired public UserMapper userMapper; public List<User> findByName(String name) { return userMapper.findUserByName(name); } public User insertUser(User user) { userMapper.insertUser(user); return user; } public List<User> ListUser(){ return userMapper.ListUser(); } public int Update(User user){ return userMapper.Update(user); } public int delete(int id){ return userMapper.delete(id); } }
controller层 的访问类CRUD:
package com.example.shujuku.controller; import java.util.List; import com.example.shujuku.entity.User; import com.example.shujuku.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping(value = "/crud", method = { RequestMethod.GET, RequestMethod.POST }) public class CRUD { @Autowired private UserService userservice; @RequestMapping("/ListUser") @ResponseBody public List<User> ListUser(){ return userservice.ListUser(); } @RequestMapping("/ListUserByname") @ResponseBody public List<User> ListUserByname(String name){ return userservice.findByName(name); } @RequestMapping(value = "/delete", method = RequestMethod.GET) public String delete(int id) { int result = userservice.delete(id); if (result >= 1) { return "删除成功"; } else { return "删除失败"; } } @RequestMapping(value = "/update", method = RequestMethod.POST) public String update(User user) { int result = userservice.Update(user); if (result >= 1) { return "修改成功"; } else { return "修改失败"; } } @RequestMapping(value = "/insert", method = RequestMethod.POST) public User insert(User user) { return userservice.insertUser(user); } }
接着:
在src/main/resources下写UserMapper的映射文件xml.
UserMapper.xml
<?xml version = "1.0" encoding = "UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD com.example.Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.shujuku.mapper.UserMapper"> <resultMap id="result" type="com.example.shujuku.entity.User"> <result property="name" column="name" /> <result property="password" column="password" /> <result property="number" column="number"/> </resultMap> <select id="ListUser" resultMap="result"> SELECT * FROM user </select> <select id="findUserByName" resultMap="result"> SELECT * FROM user where name=#{name} </select> <insert id="insertUser" parameterType="com.example.shujuku.entity.User" keyProperty="id" useGeneratedKeys="true"> INSERT INTO user ( id,name,password,number ) VALUES ( #{id}, #{name, jdbcType=VARCHAR}, #{password, jdbcType=VARCHAR}, #{number} ) </insert> <delete id="delete" parameterType="int"> delete from user where id=#{id} </delete> <update id="Update" parameterType="com.example.shujuku.entity.User"> update user set user.name=#{name},user.password=#{password},user.number=#{number} where user.id=#{id} </update> </mapper>
最后配置application.properties配置文件:
spring.datasource.url = jdbc:mysql://localhost:3306/world spring.datasource.username = root spring.datasource.password = 123456 spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.max-active=20 spring.datasource.max-idle=8 spring.datasource.min-idle=8 spring.datasource.initial-size=10 mybatis.mapper-locations= classpath:mapper/*.xml
启动程序的入口类:SpringbootMybatisApplication.java
测试查询所有的信息:http://localhost:8080/CRUD/ListUser