1. 创建一个简单的Spring Boot项目。
2. 配置项目的依赖,包括Spring Boot和MySQL连接驱动
<?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.1.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo1114</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo0310164</name>
<description>demo0310164</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.1</version>
</dependency>
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>3.1.2</version>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.6.4</version>
</dependency>
<dependency>
<groupId>org.webjars</groupId>
<artifactId>webjars-locator-core</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>10.1.13</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
3. 使用JdbcTemplate创建一个MySQL数据库,并设计一个适当的数据表。
4. 编写Spring Boot的配置文件,配置JdbcTemplate与MySQL的连接信息。
5. 在项目中创建一个DAO类,使用JdbcTemplate完成对数据库的增删改查操作。
使用JdbcTemplate完成对数据库的增删改查操作。
@Service
public class UserDao {
JdbcTemplate template;
@Autowired
public UserDao(JdbcTemplate template) {
this.template = template;
}
public List<User> getAll() {
String sql = "select * from user";
return template.query(sql, (rs, rowNum) -> {
User entity = new User();
entity.setId(rs.getInt("id"));
entity.setName(rs.getString("name"));
return entity;
});
}
//根据id获取用户信息
public User getById(Integer id) {
String sql = "select * from user where id=?";
return template.queryForObject(sql, new Object[]{id},
new BeanPropertyRowMapper<>(User.class));
}
//增加数据
public int add(User entity) {
String sql = "insert into user(name) values(?)";
return template.update(sql, entity.getName());
}
//修改数据
public int update(User entity) {
String sql = "update user set name=? where id=?";
return template.update(sql, entity.getName(), entity.getId());
}
//删除数据
public int delete(Integer id) {
String sql = "delete from user where id=?";
return template.update(sql, id);
}
}
————————————————
版权声明:本文为CSDN博主「塔塔开786」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https:///2202_75349739/article/details/133444311
6. 在项目中创建一个Controller类,提供相应的接口访问数据库操作
7. 实现以下功能:
- 插入一条数据到数据库。
- 查询数据库中的所有数据。
- 根据条件查询数据库中的数据。
- 更新数据库中的数据。
- 删除数据库中的数据。