请注意,本文编写于 1396 天前,最后修改于 741 天前,其中某些信息可能已经过时。
新建SpringBoot项目
勾选 Lombok,Web,Mybatis,Mysql 依赖
pom.xml
<dependencies>
<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>2.1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
创建数据库和数据表
CREATE TABLE student(
id INT PRIMARY KEY auto_increment,
name VARCHAR(10),
sex int,
birthday date
);
INSERT INTO student(name,sex,birthday) values
("小明",1,"2020-1-1 00:00:00"),
("小红",0,"2020-2-8 00:00:00"),
("小白",0,"2020-3-4 00:00:00");
entity
在 entity 文件夹中,写实体类
@Data
public class Student {
private int id;
private String name;
private int sex;
private Date birthday;
}
mapper
在 mapper 层中写 StudentMapper 接口
@mapper
public interface StudentMapper {
/**
* 增加一个新学生
* @param student
*/
void save(Student student);
/**
* 通过id删除一个学生
* @param id
*/
void delete(int id);
/**
* 根据id修改学生
* @param student
*/
void update(Student student);
/**
* 查询所有学生
* @return 学生
*/
List<Student> findAll();
/**
* 通过id查询学生
* @param id
* @return 学生
*/
Student findById(int id);
}
Mapper.xml
在 resources/mapping 中创建 StudentMapper 接口的 Mapper.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.example.mapper.StudentMapper">
<select id="findAll" resultType="student">
select * from student
</select>
<select id="findById" resultType="student" >
select * from student where id = #{id}
</select>
<insert id="save" parameterType="student">
insert into student(name,sex,birthday) values (#{name},#{sex},#{birthday})
</insert>
<update id="update" parameterType="student">
update student set name=#{name},sex=#{sex},birthday=#{birthday} where id = #{id}
</update>
<delete id="delete">
delete from student where id = #{id}
</delete>
</mapper>
controller
在 controller 层新建 StudentHandler 类
注意: 这里注入时bean会提示不能注入,可以在StudentMapper中添加 @Mapper 注解,或者在启动类中添加扫描包
eg: @MapperScan("com.example.mapper")
@RestController
public class StudentHandler {
@Autowired
private StudentMapper studentMapper;
@GetMapping("/findAll")
public List<Student> findAll(){
return studentMapper.findAll();
}
@GetMapping("/findById/{id}")
public Student findById(@PathVariable("id") int id){
return studentMapper.findById(id);
}
@PutMapping("/save")
public void save(@RequestBody Student student){
studentMapper.save(student);
}
@PutMapping("/update")
public void update(@RequestBody Student student){
studentMapper.update(student);
}
@DeleteMapping("/delete/{id}")
public void delete(@PathVariable("id") int id){
studentMapper.delete(id);
}
}
application.yml
在 resources 文件夹新建 application.yml
填写数据库信息
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
mybatis:
type-aliases-package: com.example.entity
mapper-locations: classpath:/mapping/*.xml
在启动类中添加扫描mapper
//这里你接口中都有mapper注解的话,可以不写,两者功能相同。
@MapperScan("com.example.mapper") //mapper包的路径
运行并测试
版权属于:乐心湖's Blog
本文链接:https://xn2001.com/archives/364.html
声明:博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!
5 comments
IDEA 右键 Generate 不是可以直接生成吗?
注解更加方便,而且不会显示在代码区里面。idea虽然生成了,但是你展示出来的话导致你的实体类代码看起来很多,后期维护很不友好。
测试邮件
Lombok干嘛的呢,没用过
自动写好Get Set toString 等等的 -,只需要一个注解,不满意有的话也可以自己覆盖