Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

SpringBoot (11): integrated Mybatis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

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:

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report