idea+SpringBoot整合Mybatis完成增刪改查功能
阿新 • • 發佈:2018-12-15
參考地址:https://blog.csdn.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"?> <!DOCTYPEmapperPUBLIC"-//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