Brief introduction
This article introduces the SpringBoot integrated Mybatis, database connection pool using alibaba's druid. SpringBoot incognito framework does not support the use of the integrated mybaits xml although you can write files to sql, but got used to the xml configuration xml sql can also be used to achieve. The realization of what specific way does not matter, mainly to build again, for the operation of the framework is relatively clear. This blog is a way to achieve write xml sql Mybatis, do not comment manner.
pom configuration file
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.0.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.meng</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>db_demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!--自定义连接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
SpringBoot profile
the Spring: the DataSource: username: root password: 123456 url: jdbc: MySQL: //192.168.0.157: 3306 / db1 Driver-class-name: com.mysql.cj.jdbc.Driver of the type: com.alibaba.druid.pool. DruidDataSource # data sources other configuration # initial size, minimum, maximum initialSize:. 5 minIdle:. 5 for maxActive: 20 is # configuration obtaining connection waiting timeout time maxWait: 60000 # intervals the frequency of such detection, the detection needs to close idle connections, the unit milliseconds timeBetweenEvictionRunsMillis: 60000 # configure a minimum connection time survival pool milliseconds minEvictableIdleTimeMillis: 300000 validationQuery: the SELECT the FROM. 1 the DUAL testWhileIdle: to true testOnBorrow: to false testOnReturn: false poolPreparedStatements: to true # configure the monitoring statistics intercepted filters, after removing the monitoring interface sql not statistics, 'wall' for Firewall Filters: STAT, Wall, log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: to true ConnectionProperties: druid.stat.mergeSql = to true; Druid. = 500 stat.slowSqlMillis MyBatis: config-LOCATION: CLASSPATH: MyBatis / MyBatis-the config.xml Mapper-locations: CLASSPATH: MyBatis / Mapper / * XML.
And then begin to create an entity class implementation process
Creating packages: entity, mapper, controller. Create a folder under the resource mapping for writing sql statement can also be written directly in the mapper java file, paste the code directly below
Application
@MapperScan ( "com.meng.demo.mapper") // scan path following interface @SpringBootApplication public class DbDemoApplication { public static void main (String [] args) { SpringApplication.run (DbDemoApplication.class, args); } }
javaBean objects
public class EmployeePO { private Integer id; private String lastName; private String email; private Integer gender; private Integer d_id;
Mapper
interface EmployeeMapper {public public EmployeePO getById (ID Integer); // The query ID public void INSERT (EmployeePO employeePO); // Add }
Controller
@RestController public class DemoController { @Autowired EmployeeMapper employeeMapper; @GetMapping("/emp/{id}") public EmployeePO getEmp(@PathVariable("id") Integer id){ return employeeMapper.getById(id); } @GetMapping("/emp") public EmployeePO inset(EmployeePO employeePO){ employeeMapper.insert(employeePO); return employeePO; } }
Mapper.xml profile
<?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.meng.demo.mapper.EmployeeMapper"> <select id="getById" resultType="com.meng.demo.entity.EmployeePO"> SELECT a.* FROM employee a WHERE a.id = #{id} </select> <insert id="insert" keyProperty="id" useGeneratedKeys="true"> insert into employee(lastName,email,gender,d_id) values (#{lastName},#{email},#{gender},#{d_id}) </insert> </mapper>
mybatis-config.xml configuration file
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- 驼峰命名 --> </settings> </configuration>
The final architecture
The last test start
In the browser, enter: HTTP: // localhost: 8080 / emp lastName=%E6%B5%8B%E8%AF%9505&[email protected]&gender=10&d_id=1? Inserts a row
In the browser, enter: HTTP: // localhost: 8080 / emp / 5 query a data