1:导入pom组件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2:配置yml
spring:
datasource:
username: root
password: root
url: jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
3:编写一个实体bean类entity映射
/**
* 实体类bean 用来映射jpa方式的数据表
* 使用jpa注解配置映射关系
*/
@Table(name = "user") //声明表明
@Entity //为jpa声明为实体类
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "last_name")
private String lastName;
@Column
private String email;
public User(Integer id, String lastName, String email) {
this.id = id;
this.lastName = lastName;
this.email = email;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
4:表写Dao层接口
/**
* 继承jpa repository完成对数据库的操作 User是第三步的映射bean Interger是主键的数据类型
*/
public interface UserRepository extends JpaRepository<User, Integer> {
}
5:可以扩展controller层或直接使用UserRepository自动注入来操作表数据
二:Mybatis 使用mapper
1:引入pom
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
2:同上 配置文件配置数据库信息
3:定义Bean类
package com.example.springboot_demo.bean;
/**
* 实体类bean映射表
*/
public class User{
public User() {
}
public User(Integer id, String email, String last_name) {
this.id = id;
this.email = email;
this.last_name = last_name;
}
private Integer id;
private String email;
private String last_name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
}
4:定义Mapper映射关系
package com.example.springboot_demo.mapper;
import com.example.springboot_demo.bean.User;
import org.apache.ibatis.annotations.*;
@Mapper
public interface UserMapper {
@Select({"select * from department where id=#{id}"})
User getDeptById(Integer var1);
@Delete({"delete from department where id=#{id}"})
int deleteDeptById(Integer var1);
@Options(
useGeneratedKeys = true,
keyProperty = "id"
)
@Insert({"insert into department(departmentName) values(#{departmentName})"})
int insertDept(User var1);
@Update({"update department set departmentName=#{departmentName} where id={#id}"})
int updateDept(User var1);
}
三:Mybatis xml配置
1:在第二部的前提环境下该为xml方式 先配置配置项
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #指定全局配置文件位置
mapper-locations: classpath:mybatis/mapper/*.xml #指定sql映射文件位置
configuration:
map-underscore-to-camel-case: true #设置为列名允许映射驼峰命名法
2:UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.springboot_demo.mapper.UserMapper">
<insert id="insert_2">
insert into user (email,last_name)values(#{email},#{last_name})
</insert>
</mapper>
3:mapper接口中定义对应方法 即可调用