(1) The introduction of the jar package in Pom.xml, as follows: There is no need to introduce SPRING-BOOT-STARTER-JDBC dependencies, because this dependency is already included in Mybatis-spring-boot-starter
<dependency> <groupId>mysql</groupId> <artifactid>mysql-connector-java</ artifactid> </dependency> <dependency> <groupid>org.mybatis.spring.boot</ groupid> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</ Version> </dependency>
(2) in the Application.properties configuration database connection information, as follows:
#mysql数据库配置spring. datasource.url=jdbc:mysql://172.31.19.20:3306/springbootspring.datasource.username= Rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.driver
(3) Create a new user entity class as follows:
Package Springboot.domain;public class User { private String ID; private String name; Private String age; Public String GetName () { return name; } public void SetName (String name) { this.name = name; } Public String Getage () { return age; } public void Setage (String age) { this.age = age; } Public String getId () { return ID; } public void SetId (String id) { this.id = ID; } @Override public String toString () { return ' User [id= + ID + ', name= "+ name +", age= "+ Age +"] "; } }
(4) New Usermapper class
Package Springboot.dao;import Org.apache.ibatis.annotations.delete;import Org.apache.ibatis.annotations.Insert; Import Org.apache.ibatis.annotations.mapper;import Org.apache.ibatis.annotations.param;import Org.apache.ibatis.annotations.select;import Org.apache.ibatis.annotations.update;import springboot.domain.User;@ Mapperpublic interface Usermapper { @Insert ("Insert into User (Name,age) VALUES (#{name},#{age})") int AddUser ( @Param ("name") string name, @Param ("Age") of string age); @Select ("SELECT * from user where ID =#{id}") user FindByID (@Param ("id") String ID); @Update ("Update user set Name=#{name} where Id=#{id}") void Updatabyid (@Param ("id") string ID, @Param ("name") string name); @Delete ("Delete from user where Id=#{id}") void Deletebyid (@Param ("id") String ID); }
Using the MyBatis annotated version here, it is quite handy to have an XML file that corresponds to the DAO layer, which is less than before.
(5) The service layer code is as follows:
Package Springboot.service;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.stereotype.service;import Springboot.dao.usermapper;import springboot.domain.User;@ Servicepublic class UserService { @Autowired private usermapper usermapper; Public User FindByID (String id) { return Usermapper.findbyid (ID); } public int AddUser (String name,string age) { return usermapper.adduser (name,age); } public void Updatabyid (String id,string name) { Usermapper.updatabyid (id,name); } public void Deletebyid (String id) { usermapper.deletebyid (id);} }
(6) Controller layer, the code is as follows:
Package Springboot.web;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.web.bind.annotation.requestmapping;import Org.springframework.web.bind.annotation.requestparam;import Org.springframework.web.bind.annotation.restcontroller;import Springboot.domain.user;import Springboot.service.UserService; @RestControllerpublic class Hellocontroller {@Autowired private userservice user Service; @RequestMapping ("/adduser") public int adduser (@RequestParam ("name") of string name, @RequestParam ("Age") of string age) { Return Userservice.adduser (name, age); } @RequestMapping ("/finduser") public User Finduser (@RequestParam ("id") String ID) {return userservice.findby ID (ID); } @RequestMapping ("/updatabyid") public string Updatabyid (@RequestParam ("id") string ID, @RequestParam ("name") string Name) {try {Userservice.updatabyid (ID, name); } catch (Exception e) {return "error"; } Return "Success"; } @RequestMapping ("/deletebyid") public string Deletebyid (@RequestParam ("id") string id) {try { Userservice.deletebyid (ID); } catch (Exception e) {return "error"; } return "Success"; }}
Springboot and MyBatis Integration