整合mybatis
引入jar包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
使用springboot给数据库建表,执行sql
spring:
datasource:
schema:
- classpath:sql/deparmat.sql
- classpath:sql/employee.sql
注解版
无须任何配置,可直接进行开发
@Mapper
public interface DepartmentMapper {
//@Options(useGeneratedKeys = true,keyProperty = "id") 使用自增主键并将主键封装到对象中
@Options(useGeneratedKeys = true,keyProperty = "id")
@Insert("insert into department(departmentName) values(#{departmentName})")
public int insertDept(Department department);
}
@RestController
public class DeptController {
@Autowired
DepartmentMapper departmentMapper;
@GetMapping
public Department insertDept(Department department){
departmentMapper.insertDept(department);
return department;
}
//参数在访问路径中 {id}
@GetMapping("/dept/{id}")
public Department selectDept(@PathVariable("id") Integer id){
return null;
}
}
若数据库中的字段命名aa_bb,而属性则是aaBb,怎么直接对应呢,采用自定义设置mybatis的驼峰命名法
//@MapperScan(value="com.xxx.mapper")可以配置到各个配置类上都可以
@MapperScan(value="com.xxx.mapper")//批量扫描包,不用在每个Mapper类或接口上面加@mapper
@Configuration
public class CustomConfig {
@Bean
public ConfigurationCustomizer configurationCustomizer(){
return new ConfigurationCustomizer() {
@Override
public void customize(org.apache.ibatis.session.Configuration configuration) {
configuration.setMapUnderscoreToCamelCase(true);//设置驼峰命名
}
};
}
}
xml配置方式
即将sql写到xml文件中
<!--mybatis全局配置文件 mybatis-config.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="Log4J"/>
<!-- 开启驼峰命名法 -->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
</configuration>
//映射文件
<?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.xxx.DepartmentMapper">
<insert id="insertEmp">
insert into 表名(字段1,字段2) values(#{},#{})
</insert>
</mapper>
然后在application.yml中配置
mybatis:
config-location: classpath:mybatis/mybatis-config.xml //这是主配置文件路径
mapper-location: classpath:mybatis/mapper/*.xml //数据库映射文件的配置
整合SpringData JPA
Jpa(java 持久化api):JPA 是一种 ORM 规范,是 Hibernate 功能的一个子集
jpa:orm(object Relation Mapping)
首先要引入jpa jar包
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>2.1.9.RELEASE</version>
</dependency>
1)/编写一个实体类(bean)和数据表进行映射,并且配置好映射关系
//使用jpa配置映射关系
@Entity//告诉jpa是一个实体类(和数据表映射的类)
@Table(name = "tb_user")//用来指定对应的表,如果省略默认是user表
public class User {
@Id//这是一个主键
@GeneratedValue(strategy = GenerationType.IDENTITY)//自增主键
private Integer id;
@Column(name = "last_name",length = 50)//这是和数据库对应的一个列
private String lastName;
@Column//省略默认列名就是属性名
private String email;
}
2)编写dao接口来操作实体类
//yml中配置
spring:
jpa:
hibernate:
ddl-auto: update //更新或者创建数据库表结构
show-sql: true //控制台显示sql
//继承JpaRepository来完成对数据库的操作
public interface UserRepository extends JpaRepository<User,Integer>{
}