SpringBoot整合mysql

环境

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 

表内数据


SpringBoot整合mysql_第1张图片
image

pom文件配置


        org.springframework.boot
        spring-boot-starter-parent
        2.0.1.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-jetty
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-devtools
        

        
        
            javax.servlet
            jstl
        
        
            org.apache.tomcat.embed
            tomcat-embed-jasper
            
        
        
            mysql
            mysql-connector-java
        
        
            c3p0
            c3p0
            0.9.1.2
        

        
        
            commons-dbutils
            commons-dbutils
            1.6
        

    

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

   

   
    
            root
            root
            com.mysql.jdbc.Driver
            
            30000
            5
            30
            10
            5
            0
            true 
    

步骤一 在主包下创建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 queryUser() {

        QueryRunner runner = new QueryRunner(C3p0Util.getDataSource());
        String sql = "select * FROM `user` ";
        List resList = null;
        try {
            resList = runner.query(sql, new BeanListHandler(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 li = ud.queryUser();
        for (User user : li) {
            System.out.println(li);
        }
    }
}

启动程序

SpringBoot整合mysql_第2张图片
image

image

成功!!!


SpringBoot整合mysql_第3张图片
image

你可能感兴趣的:(SpringBoot整合mysql)