spring boot整合mybatis使用c3p0数据源连接mysql

时间:2021-09-22 18:44:56

刚刚接触springboot,对很多东西都不熟悉,例如,它的注解方式,他的配置方式等;听说它很牛逼,所以就尝试着去学习。在基本熟悉springboot的第一个程序之后。想到当时spring整合mybatis时使用了数据源连接数据库,所以自己也想尝试使用c3p0连接数据库。所以就有了以下的内容:

首先第一步,创建maven项目导入包:

pom.xml

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<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>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>

应用服务类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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")//扫描mapper包
@EnableAutoConfiguration//自动载入应用程序所需的所有Bean,当使用Exclude这个属性时,是禁止自动配置某个类
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

配置文件内容(配置数据库)

?
1
2
3
4
5
datasource:
 url: jdbc:mysql://ip:port/databaseName
 driver-class: com.mysql.jdbc.Driver
 username: ***
 password: ***

准备无误,尝试启用应用服务,没有报错即可继续配置数据源,尝试很多方法,自己注入是不可能的,所以,我就想到spring的配置,尝试按照那样的方式去配置,所以,DataSourceConfig.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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;
 
@Configuration
public class DataSourceConfig {
 
  @Bean(name="dataSource")
  @Qualifier(value="dataSource")//限定描述符除了能根据名字进行注入,但能进行更细粒度的控制如何选择候选者
  @Primary//用@Primary区分主数据源
  @ConfigurationProperties(prefix="c3p0.primary")//指定配置文件中,前缀为c3p0的属性值
  public DataSource dataSource(){
    return DataSourceBuilder.create().type(ComboPooledDataSource.class).build();//创建数据源
  }
  /**
  *返回sqlSessionFactory
  */
  @Bean
  public SqlSessionFactoryBean sqlSessionFactoryBean(){
    SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();
    sqlSessionFactory.setDataSource(dataSource());
    return sqlSessionFactory;
  }
}

也许会有疑问,为什么这里只需要一个sqlSessionFactory,这个具体要去熟悉spring整合mybatis。还有看看底层就明白了
这样就可以自动装配了。

数据源都准备好了,尝试连接数据库吧

编辑接口

ExamPleMapper.java

?
1
2
3
4
5
import java.util.List;
 
public interface ExamPleMapper {
  public List<Users> getUsers();
}

编辑Mapper xml文件

?
1
2
3
4
5
6
7
8
9
<?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.*.ExamPleMapper">
  <select id="getUsers" resultType="com.*.Users">
    select * from users
  </select>
</mapper>

编辑实体类,因为要将结果反射到bean类中
Users.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
  }
 
}

编辑实现

Example2.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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")//映射
public class Example2 {
  @Autowired
  public ExamPleMapper examPleMapper;
 
  @ResponseBody
  @RequestMapping(value="/gu")
  public List<Users> getUsers(){
    return examPleMapper.getUsers();
  }
}

输入地址:http://localhost:8080/example2/gu

面显示结果:
[{“userid”:1,”username”:”as”},{“userid”:2,”username”:”we”},{“userid”:3,”username”:”zx”}]

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/maiyikai/article/details/70791911