安装了idea软件且本地通过cmd 命令启动了mysql。还安装了java sdk。
总结spring-boot通过resource下的mapping文件下的文件xml语法来增删查改(因为使用了MyBatis,MyBatis 的真正强大在于它的语句映射,这是它的魔力所在。由于它的异常强大,映射器的 XML 文件就显得相对简单),xml语法的属性id和resultType与java下的文件名和path有关系
通过idea建立spring-boot项目,java 1.8 spring-boot 2.6.13。项目文件目录
项目第一次运行时,得通过idea软件右键执行sql文件
项目运行时得run main方法文件(FirstJavaProApplication)
sql文件夹下的createUser.sql
CREATE TABLE `user` (
`id` int NOT NULL,
`name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`salary` double(10,2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8_unicode_ci;
src/main/java/com.byd.firstjavapro/controller/UserController.java
package com.byd.firstjavapro.controller;
import com.byd.firstjavapro.entity.User;
import com.byd.firstjavapro.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.xml.ws.Service;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//通过id得到用户信息
// demo http://localhost:3000/user/getUser/19
@RequestMapping(value = "/getUser/{id}", method = RequestMethod.GET)
public String getUser(@PathVariable int id){
return userService.getUser(id).toString();
}
//通过id删除用户信息
// http://localhost:3000/user/delete?id=1
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public String delete(int id){
int result = userService.delete(id);
if(result >= 1){
return "删除成功!";
}else{
return "删除失败!";
}
}
//更改用户信息
//http://localhost:3000/user/update?id=19&name=小王&salary=12000
@RequestMapping(value = "/update", method = RequestMethod.GET)
public String update(User user){
int result = userService.update(user);
if(result >= 1){
return "更新成功!";
}else{
return "更新失败!";
}
}
//插入用户信息
// http://localhost:3000/user/insert?id=15&name=浩子&salary=13000
@RequestMapping(value = "/insert", method = RequestMethod.GET)
public int insert(User user){
return userService.save(user);
}
//查询所有用户的信息
// http://localhost:3000/user/selectAll
@RequestMapping(value = "/selectAll")
@ResponseBody //理解为:单独作为响应体,这里不调用实体类的toString方法
public List<User> listUser(){
return userService.selectAll();
}
}
src/main/java/com.byd.firstjavapro/entity/User.java
package com.byd.firstjavapro.entity;
/**
* @description: 实体类
* @author: Fish_Vast
* @Date: 2021/8/25
* @version: 1.0
*/
public class User {
private String name;
private Integer id;
private Double salary;
public User() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", id=" + id +
", salary=" + salary +
'}';
}
}
src/main/java/com.byd.firstjavapro/mapper/UserMapper.java
package com.byd.firstjavapro.mapper;
import com.byd.firstjavapro.entity.User;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface UserMapper {
//1.通过id查询用户信息
User getUser(int id);
//2.通过id删除用户信息
int delete(int id);
//3.更改用户信息
int update(User user);
//4.插入用户信息
int save(User user);
//5.查询所有用户信息
List<User> selectAll();
}
src/main/java/com.byd.firstjavapro/service/UserService.java
package com.byd.firstjavapro.service;
import com.byd.firstjavapro.entity.User;
import com.byd.firstjavapro.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public User getUser(int id){
return userMapper.getUser(id);
}
public int delete(int id){
return userMapper.delete(id);
}
public int update(User user){
return userMapper.update(user);
}
public int save(User user){
return userMapper.save(user);
}
public List<User> selectAll(){
return userMapper.selectAll();
}
}
src/main/java/com.byd.firstjavapro/FirstJavaProApplication.java
package com.byd.firstjavapro;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.mybatis.spring.annotation.MapperScan;
@MapperScan("com.byd.firstjavapro.mapper")
@SpringBootApplication
public class FirstJavaProApplication {
public static void main(String[] args) {
SpringApplication.run(FirstJavaProApplication.class, args);
}
}
下面是main文件下resources文件夹
src/main/resources/mapping/UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.byd.firstjavapro.mapper.UserMapper">
<resultMap id="BaseResultMap" type="com.byd.firstjavapro.entity.User">
<result column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="salary" jdbcType="DOUBLE" property="salary" />
</resultMap>
<!--查询用户信息-->
<select id="getUser" resultType="com.byd.firstjavapro.entity.User">
select * from user where id = #{id}
</select>
<!--删除用户信息-->
<delete id="delete" parameterType="int">
delete from user where id=#{id}
</delete>
<!--返回所有用户信息-->
<select id="selectAll" resultType="com.byd.firstjavapro.entity.User">
select * from user
</select>
<!--增加用户信息-->
<insert id="save" parameterType="com.byd.firstjavapro.entity.User" >
insert into user
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="id != null" >
id,
</if>
<if test="name != null" >
name,
</if>
<if test="salary != null" >
salary,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="id != null" >
#{id,jdbcType=INTEGER},
</if>
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="salary != null" >
#{salary,jdbcType=DOUBLE},
</if>
</trim>
</insert>
<!--根据id更改用户信息-->
<update id="update" parameterType="com.byd.firstjavapro.entity.User">
update user
<set >
<if test="name != null" >
name = #{name,jdbcType=VARCHAR},
</if>
<if test="salary != null" >
salary = #{salary,jdbcType=DOUBLE},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
</mapper>
src/main/resources/static/index.html
<html>
<body>
<h1>hello word!!!</h1>
<p>this is a html page</p>
</body>
</html>
src/main/resources/application.properties
server.port=3000
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mybatis??
mybatis.mapper-locations= classpath:mapping/*.xml
mybatis.type-aliases-package= com.byd.firstjavapro.entity
根目录下的
pom.xml文件(从这个文件可以看出这个项目用的java1.8版本,spring-boot版本2.16.13版本)
<?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>
<groupId>com.byd</groupId>
<artifactId>firstJavaPro</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>firstJavaPro</name>
<description>firstJavaPro</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.6.13</spring-boot.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-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
<configuration>
<mainClass>com.byd.firstjavapro.FirstJavaProApplication</mainClass>
<skip>true</skip>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>