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

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

<parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.4.1.RELEASEversion>
    parent>

    <dependencies>
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <version>1.4.1.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <version>1.4.1.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.mybatis.spring.bootgroupId>
            <artifactId>mybatis-spring-boot-starterartifactId>
            <version>1.1.1version>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.33version>
        dependency>
        <dependency>
            <groupId>c3p0groupId>
            <artifactId>c3p0artifactId>
            <version>0.9.1.2version>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

应用服务类

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);
    }
}

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

datasource: 
  url: jdbc:mysql://ip:port/databaseName
  driver-class: com.mysql.jdbc.Driver
  username: ***
  password: ***

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

@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

import java.util.List;

public interface ExamPleMapper {
    public List getUsers();
}

编辑Mapper xml文件



<mapper namespace="com.*.ExamPleMapper">
    <select id="getUsers" resultType="com.*.Users">
        select * from users
    select>
mapper>

编辑实体类,因为要将结果反射到bean类中
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
    }

}

编辑实现
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")//映射
public class Example2 {
    @Autowired
    public ExamPleMapper examPleMapper;

    @ResponseBody
    @RequestMapping(value="/gu")
    public List getUsers(){
        return examPleMapper.getUsers();
    }
}

输入地址:http://localhost:8080/example2/gu
页面显示结果:
[{“userid”:1,”username”:”as”},{“userid”:2,”username”:”we”},{“userid”:3,”username”:”zx”}]

你可能感兴趣的:(spring-boot,java,mysql,数据库)