I just came into contact with springboot and am not familiar with many things, such as its annotation method, its configuration method, etc.; I heard that it is very awesome, so I tried to learn it. After basically getting familiar with the first program of springboot. I thought that when spring integrated mybatis, it used data sources to connect to the database, so I also wanted to try using c3p0 to connect to the database. So there is the following content:
First, the first step is to create a maven project import package:
pom.xml
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.4.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>1.4.1.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.33</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> </dependencies> <build> <plugins> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Application Services
import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@MapperScan(value="com.myk.spring.t3")//Scan the mapper package @EnableAutoConfiguration//Autoload all beans required for the application. When using the Exclude property, it is prohibited to automatically configure a certain class public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}Configuration file content (configuration database)
datasource: url: jdbc:mysql://ip:port/databaseName driver-class: com.mysql.jdbc.Driver username: *** password: ***
Prepare correctly, try to enable the application service, and continue to configure the data source without an error. Try many methods, it is impossible to inject it yourself, so I thought of the configuration of spring and try to configure it in that way. Therefore, DataSourceConfig.java
import javax.sql.DataSource;import org.mybatis.spring.SqlSessionFactoryBean;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.Primary;import com.mchange.v2.c3p0.ComboPooledDataSource;@Configurationpublic class DataSourceConfig { @Bean(name="dataSource") @Qualifier(value="dataSource")//In addition to injecting according to the name, the qualified descriptor can perform finer granular control.How to select candidates @Primary//Use @Primary to distinguish the main data source @ConfigurationProperties(prefix="c3p0.primary")//Specify the property value prefixed with c3p0 in the configuration file public DataSource dataSource(){ return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();//Create data source} /** *Return sqlSessionFactory */ @Bean public SqlSessionFactoryBean sqlSessionFactoryBean(){ SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean(); sqlSessionFactory.setDataSource(dataSource()); return sqlSessionFactory; }} There may be questions, why only a sqlSessionFactory is needed here, and you need to be familiar with spring integration mybatis. Also, look at the bottom layer and understand that this way you can automatically assemble.
The data source is ready, try connecting to the database
Edit interface
ExamPleMapper.java
import java.util.List;public interface ExamPleMapper { public List<Users> getUsers();}Edit Mapper xml file
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.*.ExamPleMapper"> <select id="getUsers" resultType="com.*.Users"> select * from users </select></mapper>
Edit the entity class because you want to reflect the result into the bean class
Users.java
public class Users { private int userid; private String username; @Override public String toString() { return "Users [userid=" + userid + ", username=" + username + "]"; } public int getUserid() { return userid; } public void setUserid(int userid) { this.userid = userid; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public Users(int userid, String username) { super(); this.userid = userid; this.username = username; } public Users() { super(); // TODO Auto-generated constructor stub }} Editing and Implementation
Example2.java
import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.bind.annotation.RestController;@RestController@RequestMapping(value="/example2")//Mapping public class Example2 { @Autowired public ExamPleMapper examPleMapper; @ResponseBody @RequestMapping(value="/gu") public List<Users> getUsers(){ return examPleMapper.getUsers(); }} Enter address: http://localhost:8080/example2/gu
Display results on the surface:
[{"userid":1,"username":"as"},{"userid":2,"username":"we"},{"userid":3,"username":"zx"}]
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.