Get the App
SLTechnology News&Howtos  ›  Database  › 

SpringBoot (11): integrated Mybatis

Shulou Source: shulou.com Published: 2022-06-01 15:03:41 09月15日 Update

Add dependencies

Org.mybatis.spring.boot mybatis-spring-boot-starter 1.2.0 mysql mysql-connector-java runtime

II. Integration based on mybais annotations

2.1. Configure the data source

# # mysql data Source configuration # # spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver

2.2, java code

User.java

Package com.example.demo.pojo;import java.io.Serializable;import java.util.Date;/** * user entity class * @ Author: I love Dajin * @ Description: user entity class * @ Date: Created in 14:25 2017-6-18 * / public class User implements Serializable {private Integer id; private String name; private Date createTime; public Integer getId () {return id } public void setId (Integer id) {this.id = id;} public String getName () {return name;} public void setName (String name) {this.name = name;} public Date getCreateTime () {return createTime;} public void setCreateTime (Date createTime) {this.createTime = createTime } @ Override public String toString () {return "User {" + "id=" + id + ", name='" + name +'\'+ ", createTime=" + createTime +'}';}}

UUserMapper.java

Package com.example.demo.mapper;import com.example.demo.pojo.User;import org.apache.ibatis.annotations.*;import org.apache.ibatis.type.JdbcType / * * user Mapper * @ Author: I love Dajin * @ Description: user Mapper * @ Date: user Mapper * @ Date 14:28 2017-6-18 * / @ Mapperpublic interface UserMapper {/ * * add user * / @ Insert (value = "insert into user (name,create_time) values (# {name,jdbcType=VARCHAR}, # {createTime,jdbcType=TIMESTAMP})") int insert (User record) / * * query the user according to id * / @ Select (value = "select id, name, create_time from user where id = # {id,jdbcType=INTEGER}") @ Results (value = {@ Result (column = "create_time", property = "createTime", jdbcType= JdbcType.TIMESTAMP)}) User selectByPrimaryKey (Integer id);}

2.3. Testing

Package com.example.demo;import com.example.demo.mapper.UserMapper;import com.example.demo.pojo.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.Date;@RunWith (SpringRunner.class) @ SpringBootTestpublic class SpringbootDemo27ApplicationTests {@ Autowired private UserMapper mapper @ Test public void insert () {User user = new User (); user.setName ("Test"); user.setCreateTime (new Date ()); int result = mapper.insert (user); System.out.println (result);} @ Test public void select () {User result = mapper.selectByPrimaryKey (1); System.out.println (result);}}

Run the insert method:

Run the select method:

Third, xml integration based on mybatis

3.1, configuration

# # mysql data Source configuration # # spring.datasource.url=jdbc:mysql://localhost/db_test?useUnicode=true&characterEncoding=utf-8spring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver### # # mybatis integration based on xml # # mybatis.mapper-locations: classpath:mybatis/*.xml# alias # mybatis.type-aliases-package: com.example.demo.pojo

3.2Code of java

UUserMapper2.java

Package com.example.demo.mapper;import com.example.demo.pojo.User;import org.apache.ibatis.annotations.*;import org.apache.ibatis.type.JdbcType;/** * user Mapper * @ Author: I love Dajin * @ Description: user Mapper * @ Date: Created in 14:28 2017-6-18 * / @ Mapperpublic interface UserMapper2 {/ * * add user * / int insert (User record) / * * query users according to id * / User selectByPrimaryKey (Integer id);}

UserMapper2.xml

Id, name, create_time select from user where id = # {id,jdbcType=INTEGER} insert into user (name, create_time) values (# {name,jdbcType=VARCHAR}, # {createTime,jdbcType=TIMESTAMP})

3.3. Testing

Package com.example.demo;import com.example.demo.mapper.UserMapper;import com.example.demo.mapper.UserMapper2;import com.example.demo.pojo.User;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import java.util.Date;@RunWith (SpringRunner.class) @ SpringBootTestpublic class SpringbootDemo27ApplicationTests {@ Autowired private UserMapper2 mapper2 @ Test public void insert () {User user = new User (); user.setName ("Test 2"); user.setCreateTime (new Date ()); int result = mapper2.insert (user); System.out.println (result);} @ Test public void select () {User result = mapper2.selectByPrimaryKey (2); System.out.println (result);}}

Run the insert method:

Run the select method:

Tags: Users methods tests runs configurations I love data data sources Dajin code entities queries aliases comments Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Xiaomi NVidia macOS Shulou Information