springboot integrates mybatis instance code

  • 2020-07-21 08:01:22
  • OfStack

The & # 65279; How springboot configured the web project refer to the previous chapter to integrate mybatis.

Add mybatis dependencies to pom files:


  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>1.2.0</version>
  </dependency>

Add mysql driver:


  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>

Add druid and fastjson dependencies, using Alibaba druid connection pool


  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.0.28</version>
  </dependency>
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>1.2.30</version>
  </dependency>

Configure the data source in ES21en. yml:


spring:
  datasource:
    name: test
    url: jdbc:mysql://127.0.0.1:3306/test
    username: root
    password: 111111
    #  use druid The data source 
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

Set mapper and model scanning paths of mybatis:


mybatis:
  mapperLocations: classpath:mapper/*.xml
  typeAliasesPackage: com.yingxinhuitong.demo.model
# For more configuration, see: http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/

Next, let's create userMapper. xml,UserEntity and UserDao:

UserEntity.class


package com.yingxinhuitong.demo.model;

/**
 * Created by jack on 2017/4/20.
 */
public class UserEntity {
 private Long id;
 private String username;
 private String password;

 public Long getId() {
  return id;
 }

 public void setId(Long id) {
  this.id = id;
 }

 public String getUsername() {
  return username;
 }

 public void setUsername(String username) {
  this.username = username;
 }

 public String getPassword() {
  return password;
 }

 public void setPassword(String password) {
  this.password = password;
 }
}

UserDao


package com.yingxinhuitong.demo.dao;

import com.yingxinhuitong.demo.model.UserEntity;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * Created by jack on 2017/4/20.
 */
@Mapper
public interface UserDao {
 List<UserEntity> searchAll();
}

UserMapper.xml


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.yingxinhuitong.demo.dao.UserDao" >
 <!--  Mapping of fields to entities  -->
 <resultMap id="BaseResultMap" type="com.yingxinhuitong.demo.model.UserEntity">
  <id column="id" property="id" jdbcType="BIGINT" />
  <result column="username" property="username" jdbcType="VARCHAR" />
  <result column="password" property="password" jdbcType="VARCHAR" />
 </resultMap>
 <!--  Query by condition, all  -->
 <select id="searchAll" resultMap="BaseResultMap">
  select * from tab_user
 </select>

</mapper>

Create a controller, inject UserDao, and test whether the data can be queried under 1:


@RestController
public class TestController {

 @Resource
 UserDao userDao;

 @RequestMapping("/getusers")
 public String test() {
  List<UserEntity> users = userDao.searchAll();
  String usersJson = JSON.toJSONString(users);
  return usersJson;
 }
}

Run Application class, start after a successful visit: http: / / localhost: 9000 / demo/getusers, output content is as follows:


[{"id":1,"password":"000000","username":"test"},{"id":2,"password":"111111","username":"test1"},{"id":3,"password":"222222","username":"test2"}]

At this point, springboot has completed the integration of mybatis.


Related articles: