(一)创建项目
再选择web下的web,SQL下的MySQL,JDBC,MyBatis:
刚创建项目时的目录:
要操作的数据表(名为texttable)样式:
(二)代码
完整的项目目录:
第一步:首先编写该项目的项目配置文件,是resource文件夹下application.properties(项目创建时自动生成这个文件的),为了语法简便,我们把application.properties改成application.yml,其编写内容如下:
server:
port: 8080 #tomcat端口
spring: #springboot的配置
datasource: #定义数据源
name: test
#127.0.0.1为本机测试的ip,3306是mysql的端口号。serverTimezone是定义时区,照抄就好,mysql高版本需要定义这些东西
#useSSL也是某些高版本mysql需要问有没有用SSL连接
url: jdbc:mysql://127.0.0.1:3306/textdatabase?serverTimezone=GMT%2B8&useSSL=FALSE
username: root #数据库用户名,root为管理员
password: zhujunwen #该数据库用户的密码
mybatis: #mybatyis的配置
mapper-locations: classpath:mapper/*.xml #指定mapper的配置文件的路径是mapper文件夹下的所有 xml文件。
第二步:创建一个实体类User.class,里面包含某个数据表的一些或全部字段。
package com.example.demo;
public class User {
private Integer id;
private String name;
private Integer age;
public User(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
第三步:编写一个mapper文件,里面包含对数据库操作的所有方法(当然这些方法都是自己写的)UserMapper:
package com.example.demo;
import java.util.List;
public interface UserMapper {
int insertUser(User user);
int deleteUser(Integer id);
User selectUser(Integer id);
int updateUser(User user);
List<User> selectAll();
}
第四步:有了UserMapper里的方法定义,接下来就是要实现这些方法了,因为涉及到操作mysql数据库,所以这些方法的实现要结合SQL语句,UserMapper.xml 就是用SQL语句实现UserMapper里定义的方法的:
<?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" >
<!--上面2行的是约束依赖,固定照抄就好-->
<!--下面的才是要自己编写的地方-->
<!--写mapper的配置文件第一步就是要写<mapper></mapper>标签-->
<!--<mapper></mapper>标签里包含着各个CURD操作的SQL语句-->
<mapper namespace="com.example.demo.UserMapper">
<!--定义一个名为BaseResultMap的返回类型-->
<resultMap id="BaseResultMap" type="com.example.demo.User">
<id column="id" property="id" jdbcType="INTEGER"></id>
<result column="name" property="name" jdbcType="VARCHAR"></result>
<result column="age" property="age" jdbcType="INTEGER"></result>
</resultMap>
<!--插入语句-->
<!--id要与UserMapper文件中表示插入的函数名一致,parameterType表示函数的输入参数的类型-->
<insert id="insertUser" parameterType="com.example.demo.User">
insert into texttable(id,name,age)values(#{id,jdbcType=INTEGER},#{name,jdbcType=VARCHAR},#{age,jdbcType=INTEGER})/*SQL语句*/
</insert>
<!--删除语句-->
<delete id="deleteUser" parameterType="java.lang.Integer">
delete from texttable where id =#{id,jdbcType=INTEGER}
</delete>
<!--查找语句-->
<!--resultMap表示函数返回的类型-->
<select id="selectUser" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select * from texttable where id = #{id,jdbcType=INTEGER}
</select>
<!--查找语句-->
<select id="selectAll" resultMap="BaseResultMap">
select * from texttable
</select>
<!--修改语句-->
<update id="updateUser" parameterType="com.example.demo.User">
update texttable set name=#{name,jdbcType=VARCHAR},age=#{age,jdbcType=INTEGER} where id=#{id,jdbcType=INTEGER}
</update>
</mapper>
这里要稍微说一下上面代码的这一部分:
<resultMap id="BaseResultMap" type="com.example.demo.User">
<id column="id" property="id" jdbcType="INTEGER"></id>
<result column="name" property="name" jdbcType="VARCHAR"></result>
<result column="age" property="age" jdbcType="INTEGER"></result>
</resultMap>
<resultMap> 是为<select>定义返回的类型,因为select语句是查询语句,肯定要返回查询结果。
id=“BaseResultMap”是说定义的这个resultMap给它一个名字“BaseResultMap”,type指定返回的结果类型。
<resultMap>中的<id>是指明唯一标识是实体类(User)中的哪一个变量(column),该变量对应数据表中的哪一个字段(property)
而<resultMap>中的<result>则是指明除了id之外的字段。
第五步:编写UserService接口:
package com.example.demo;
import java.util.List;
public interface UserService {
int addUser(User user);
int deleteUser(Integer id);
User selectUser(Integer id);
int updateUser(User user);
List<User> findAll();
}
第六步:实现UserService接口,编写UserServiceImpl.class:
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper; //这里会报错,但并不影响
@Override
public int addUser(User user) {
return userMapper.insertUser(user);
}
@Override
public int deleteUser(Integer id) {
return userMapper.deleteUser(id);
}
@Override
public User selectUser(Integer id) {
return userMapper.selectUser(id);
}
@Override
public List<User> findAll() {
return userMapper.selectAll();
}
@Override
public int updateUser(User user) {
return userMapper.updateUser(user);
}
}
顺带一说会报错的原因是因为UserMapper接口的上面要加注释@Component表示这是一个Bean,但如果有多个UserMapper的话,每一个都加一个@Component就会显得麻烦,因此用另外一种替代方法:就是在主函数上面加@MapperScan
第七步:往主函数DemoApplication.class加@MapperScan:
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
@MapperScan(“com.example.demo”)表示要扫描的Mapper的目录路径是在com.example.demo下的。
第八步:编写UserController.class:
这个类的编写是已经开始测试代码的功能的,只不过通过借助浏览器罢了
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserServiceImpl userService;
@RequestMapping(value = "/findAll")
public List<User> findAll() {
return userService.findAll();
}
@RequestMapping(value = "add")
public int addUser(){
User u = new User(99,"oooo",45);
int stat = userService.addUser(u);
return stat;
}
}
运行程序(即DemoApplication.class)后,通过在浏览器上输入http://localhost:8080/user/findAll,会在浏览器显示texttable数据表中的所有数据:
同理,在浏览器上输入:http://localhost:8080/user/all 就会在texttable数据表中插入(99,“oooo”,45)的一项数据。
第九步:编写test代码来测试代码:
test是最正规的测试代码方法:
要用test,需要在项目的maven配置文件pom.xml(也是一开始就自动生成的) 中加入依赖:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
但是在实操中,需要额外注释掉一行:(就是spring-boot-starter-test 下的<scope>test</scope>)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!--<scope>test</scope>-->
</dependency>
下面是test.class的代码:
package com.example.demo;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class test {
@Autowired
private UserServiceImpl us;
@org.junit.Test
public void addUser(){
User u = new User(265,"adcass",16);
us.addUser(u);
}
@org.junit.Test
public void deleteUser(){
Integer id = 27;
us.deleteUser(id);
}
@org.junit.Test
public void selectUser(){
Integer id = 1;
User ur = us.selectUser(id);
System.out.println(ur);
}
@org.junit.Test
public void findAll(){
List<User> u_list = us.findAll();
System.out.println(u_list);
}
@org.junit.Test
public void updateUser(){
User u = new User(99,"AAAA",25);
us.updateUser(u);
}
}
每一个测试代码都有@org.junit.Test 的,要单独运行哪一个,可以在@org.junit.Test下右键运行即可。如我右键运行findAll():
效果:
项目代码下载地址:https://github.com/zhujunwen/springboot-mybatis-mysql
===============================
最后给出完整的pom.xml文件:(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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.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-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<!--<scope>test</scope>-->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>