美文网首页
SpringBoot整合mysql

SpringBoot整合mysql

作者: 左羊 | 来源:发表于2019-07-29 23:21 被阅读0次

环境

Windows 7
JDK 1.8
SpringBoot 2.0.1
Maven 3.2.5
Eclipse photon
MySQL 5.1.7
C3p0 0.9.1.2

前期准备

net start mysql

mysql -u **** -p****

create databases test;

CREATE TABLE `user` (
  `id` int(11) NOT NULL,
  `name` varchar(30) DEFAULT NULL,
  `password` varchar(30) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 

表内数据


image

pom文件配置

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- jsp -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <!--<scope>provided</scope> -->
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-dbutils/commons-dbutils -->
        <dependency>
            <groupId>commons-dbutils</groupId>
            <artifactId>commons-dbutils</artifactId>
            <version>1.6</version>
        </dependency>

    </dependencies>

添加配置文件配置数据库和其他参数
在resource文件夹下添加c3p0-config.xml配置文件并输入数据库参数,如下:

   <?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>   
    <default-config>
            <property name="user">root</property>
            <property name="password">root</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="jdbcUrl"><![CDATA[jdbc:mysql://localhost:3306/test?useSSL=false&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8]]></property>
            <property name="checkoutTimeout">30000</property>
            <property name="initialPoolSize">5</property>
            <property name="maxIdleTime">30</property>
            <property name="maxPoolSize">10</property>
            <property name="minPoolSize">5</property>
            <property name="maxStatements">0</property>
            <property name="testConnectionOnCheckout">true</property> 
    </default-config>
</c3p0-config>

步骤一 在主包下创建springBoot启动类

package bootfirst.com.dxf;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication //启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
 public static void main(String[] args) throws Exception {
  SpringApplication.run(StartSpringBootMain.class, args);
 }

}

步骤二 在主包下创建Utils包,在包下创建C3p0Util类

package com.dxf.util;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class C3p0Util {
    private static DataSource ds;
    static {
        ds = new ComboPooledDataSource();
    }
    
    public static DataSource getDataSource(){
        return ds;
    }
    
}

步骤三 在model包中创建User.java

package com.dxf.model;

public class User {
    private int id;
    private String name;
    private String password;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", name=" + name + ", password=" + password + "]";
    }
    
}

步骤四 在dao包中创建UserDAO.java

package com.dxf.dao;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.dxf.model.User;
import com.dxf.util.C3p0Util;

public class UserDAO {
    public List<User> queryUser() {

        QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
        String sql = "select * FROM `user` ";
        List<User> resList = null;
        try {
            resList = runner.query(sql, new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return resList;
    }
}

最后在StartSpringBootMain启动类中进行检测

package com.dxf;

import java.util.List;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.dxf.dao.UserDAO;
import com.dxf.model.User;
import com.dxf.util.C3p0Util;
@SpringBootApplication  // 启动SpringBoot程序,而后自带子包扫描
public class StartSpringBootMain {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartSpringBootMain.class, args);
        System.out.println(C3p0Util.getDataSource());
        UserDAO ud =new UserDAO();
        List<User> li = ud.queryUser();
        for (User user : li) {
            System.out.println(li);
        }
    }
}

启动程序

image
image

成功!!!


image

相关文章

网友评论

      本文标题:SpringBoot整合mysql

      本文链接:https://www.haomeiwen.com/subject/mpjwrctx.html