1.1 配置版本
具体使用到的各配置版本信息如下:
JAVA17 ;
SpringBoot 3.0.5 ;
MyBatis-Plus 3.5.3.1 ;
Druid 1.1.14;
MySql 5.0.8 ;
因为以前装的老版本的mysql 用的mysql-connector-java 3.1.12 jar
1.2 配置文件
pom.xml文件部分代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>Springbootdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Springbootdemo</name>
<description>Springbootdemo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<!-- 解决:No active profile set, fallingback to default profiles: default -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- MyBatis Plus 相关配置 -->
<!-- mybatis-plus 组件 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- mybatis plus 代码生成器依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.5.3.1</version>
</dependency>
<!-- mybatis plus 代码生成器模板,MyBatis-Plus 支持 Velocity(默认)、Freemarker -->
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.3</version>
</dependency>
<!-- MySQL相关配置 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>3.1.12</version>
</dependency>
<!--c3p0-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.4</version>
</dependency>
<!-- Lombox相关配置 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.14</version>
</dependency>
<!--返回html的maven依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
1.3 项目结构
项目结构如下图所示:
application.properties文件
配置信息如下:
server.port = 8080
spring.profiles.active=default
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://localhost:3306/eos?characterEncoding=utf-8
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
SpringbootdemoApplication 添加MapperScan扫描层,代码如下:
ps:如果UserMapper接口类和我上图项目结构一样,放同一目录可以不加
package com.springbootdemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
//@MapperScan("com.springbootdemo.mapper")
public class SpringbootdemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootdemoApplication.class, args);
}
}
UserMapper
package com.springbootdemo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.springbootdemo.bean.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
SpringBootController
package com.springbootdemo;
import com.springbootdemo.bean.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
public class SpringBootController {
// http://localhost:8080/springBoot/say
@RequestMapping(value = "/springBoot/say")
public @ResponseBody String say() {
return "Hello,springBoot!";
}
@Autowired
private UserMapper userMapper;
// http://localhost:8080/action
@RequestMapping("/action")
public String index(Model model){
System.out.println("action");
/* System.out.println(("----- selectAll method test ------"));
List<User> userList = userMapper.selectList(null);
userList.forEach(System.out::println);*/
model.addAttribute("a", "o");
model.addAttribute("b", "k");
return "index.html";
}
}
打开注释测试Druid 取数据
User
package com.springbootdemo.bean;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@Data
@TableName("eos.user")
public class User {
@TableId
private Integer id;
private String name;
private Integer age;
private String email;
}
index.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<strong>Hello world!</strong>
<br>
<p>测试: <span th:text="${a}"></span></p>
<p>测试: <span th:text="${b}"></span></p>
</body>
</html>
我用的老版本的mysql,测试连接数据库,在复习下jdbc,c3p0,不需要的直接跳过
jdbcTest
package com.springbootdemo.test;
import java.sql.*;
public class jdbcTest {
//驱动名称(包含了数据库的产品和数据库的版本号)
private static String driver = "com.mysql.jdbc.Driver";
//数据库的url
private static String url = "jdbc:mysql://localhost:3306/eos?characterEncoding=utf-8";
//数据库用户名
private static String user = "root";
//数据库密码
private static String pass = "123456";
//使用jdbc进行添加操作
public static void select() throws Exception {
Connection conn = null;
Statement stmt = null;
try{
// 注册 JDBC 驱动
Class.forName(driver);
// 打开链接
System.out.println("数据库...");
conn = DriverManager.getConnection(url,user,pass);
// 执行查询
System.out.println(" 对接Statement...");
stmt = (Statement) conn.createStatement();
String sql;
sql = "SELECT id FROM eos.user";
ResultSet rs = stmt.executeQuery(sql);
// 展开结果集数据库
while(rs.next()){
// 通过字段检索
int id = rs.getInt("id");
// 输出数据
System.out.print("ID: " + id);
System.out.print("\n");
}
// 完成后关闭
rs.close();
stmt.close();
conn.close();
}catch(SQLException se){
// 处理 JDBC 错误
se.printStackTrace();
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(stmt!=null) stmt.close();
}catch(SQLException se2){
}// 什么都不做
try{
if(conn!=null) conn.close();
}catch(SQLException se){
se.printStackTrace();
}
}
}
//测试类的主方法
public static void main(String[] args) throws Exception {
select();
}
}
C3p0Test
package com.springbootdemo.test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class C3p0Test {
public void c3p0DataSourceTest() {
Connection conn = null;
PreparedStatement pst = null;
// ResultSet rs = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/eos?characterEncoding=utf-8");
dataSource.setUser("root");
dataSource.setPassword("123456");
conn = dataSource.getConnection();
pst = conn.prepareStatement("SELECT id FROM eos.user");
ResultSet resultSet = pst.executeQuery();
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
// 输出数据
System.out.print("ID: " + id);
System.out.print("\n");
}
}catch(Exception e){
// 处理 Class.forName 错误
e.printStackTrace();
}finally{
// 关闭资源
try{
if(pst!=null) pst.close();
if(conn!=null) conn.close();
}catch(Exception se){
se.printStackTrace();
}
}
}
public static void main(String[] args) {
new C3p0Test().c3p0DataSourceTest();
}
}
DruidTest
package com.springbootdemo.test;
import com.alibaba.druid.pool.DruidDataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class DruidTest {
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement pst = null;
try {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/eos?characterEncoding=utf-8");
dataSource.setUsername("root");
dataSource.setPassword("123456");
conn = dataSource.getConnection();
//普通查询
pst = conn.prepareStatement("SELECT id FROM eos.user");
ResultSet resultSet = pst.executeQuery();
while (resultSet.next()) {
// 通过字段检索
int id = resultSet.getInt("id");
// 输出数据
System.out.print("ID: " + id);
System.out.print("\n");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
SpringbootdemoApplication
启动项目
数据库数据
浏览器页面显示下图