Spring Boot(二):数据库操作
本文主要讲解如何通过spring boot来访问数据库,本文会演示三种方式来访问数据库,第一种是JdbcTemplate,第二种是JPA,第三种是Mybatis。之前已经提到过,本系列会以一个博客系统作为讲解的基础,所以本文会讲解文章的存储和访问(但不包括文章的详情),因为最终的实现是通过MyBatis来完成的,所以,对于JdbcTemplate和JPA只做简单演示,MyBatis部分会完整实现对文章的增删改查。
一、准备工作
在演示这几种方式之前,需要先准备一些东西。第一个就是数据库,本系统是采用MySQL实现的,我们需要先创建一个student的表:
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student` (
`id` varchar(10) NOT NULL,
`name` varchar(10) default NULL,
`age` varchar(10) default NULL,
`schoolName` varchar(55) default NULL,
`createTime` datetime default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
后续的演示会对这个表进行增删改查。另外配置文件使用properties配置,即application.properties(你也可以使用application.yml配置文件,没什么太大的区别,如果对ymal不熟悉,有兴趣也可以查一下,比较简单)。
spring.datasource.url=jdbc:mysql://localhost:3306/databasezfy?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
最后,我们还需要建立与数据库对应的POJO类,代码如下:
public class Student implements Serializable{
private static final long serialVersionUID = 5465872778586910807L; private String id;
private String name;
private String age;
private String schoolName;
private Date createTime;
}
好了,需要准备的工作就这些,现在开始实现数据库的操作。
二、与JdbcTemplate集成
首先,我们先通过JdbcTemplate来访问数据库,这里只演示数据的插入,上一篇文章中我们已经提到过,Spring boot提供了许多的starter来支撑不同的功能,要支持JdbcTemplate我们需要引入下面的starter和连接mysql的驱动:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
现在我们就可以通过JdbcTemplate来实现数据的插入了:
public interface StudentDao {
int insertStudent(Student student);
}
@Repository
public class StudentDaoImpl implements StudentDao{ @Autowired
private NamedParameterJdbcTemplate jdbcTemplate; @Override
public int insertStudent(Student student) {
String sql = "insert into student (id,name,age,schoolName,createTime) values (:id,:name,:age,:schoolName,:createTime)";
Map map = new HashMap();
map.put("id",student.getId());
map.put("name",student.getName());
map.put("age",student.getAge());
map.put("schoolName",student.getSchoolName());
map.put("createTime",student.getCreateTime());
int result = jdbcTemplate.update(sql, map);
return result;
}
}
我们通过JUnit来测试上面的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class StudentDaoTest { @Autowired
private StudentDao studentDao; @Test
public void testInsert(){
Student student = new Student();
student.setId("1");
student.setName("zs");
student.setAge("10");
student.setSchoolName("一中");
student.setCreateTime(new Date());
studentDao.insertStudent(student);
} }
测试结果:test passed
表中也插入了一条数据:
目录结构:
三、与JPA集成
JPA是Java Persistence API的简称,中文名Java持久层API,是JDK 5.0注解或XML描述对象-关系表的映射关系,并将运行期的实体对象持久化到数据库中。
现在我们开始讲解如何通过JPA的方式来实现数据库的操作。还是跟JdbcTemplate类似,首先,我们需要引入对应的starter:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
然后我们需要对POJO类增加Entity的注解,并指定表名(如果不指定,默认的表名为student),然后需要指定表主键ID所在的get方法,这些都是JPA的知识,与Spring boot无关,如果不熟悉的话可以看下JPA的知识点,注意注解所在的包,别引错包。
@Entity
@Table(name = "student")
public class Student implements Serializable {
private static final long serialVersionUID = 5465872778586910807L; private String id;
private String name;
private String age;
private String schoolName;
private Date createTime; @Id
public String getId() {
return id;
}
.................................
.................................
}
最后,我们需要继承JpaRepository这个类,这里我们实现了两个查询方法,第一个是符合JPA命名规范的查询(方法名写的时候会有提示),JPA会自动帮我们完成查询语句的生成,另一种方式是我们自己实现JPQL(JPA支持的一种类SQL的查询),这里举例使用第一种的查询
public interface StudentRepository extends JpaRepository<Student,String>{
public Student findStudentById(String id);
}
JpaRepository<Student,String>中的参数表明实体和主键类型。
好了,我们可以再测试一下上面的代码:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class StudentRepositoryTest {
@Autowired
private StudentRepository studentRepository; @Test
public void testQuery(){
Student studentById = studentRepository.findStudentById("1");
System.out.println(studentById);
}
}
测试结果:报错
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'student0_.create_time' in 'field list'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:377)
at com.mysql.jdbc.Util.getInstance(Util.java:360)
原因:类的属性,表字段命名是驼峰命名法(createTime),Spring data jpa 在操作表的时候,生成的sql语句中却是create_time, 表字段不对照,解决方法参见:jpa数据库表实体命名规则 Unknown column 'user0_.create_time' in 'field list',生成的sql语句时无修改命名,即在application.properties配置文件中加入
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.datasource.url=jdbc:mysql://localhost:3306/databasezfy?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456 spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
在测试一下:test passed
四、与MyBatis集成
最后,我们再看看如何通过MyBatis来实现数据库的访问。同样我们还是要引入starter:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
由于该starter不是spring boot官方提供的,所以版本号于Spring boot不一致,需要手动指定。
MyBatis一般可以通过XML或者注解的方式来指定操作数据库的SQL,个人比较偏向于XML,所以,本文中也只演示了通过XML的方式来访问数据库。首先,我们需要配置mapper的目录。我们在application.properties中进行配置:
spring.datasource.url=jdbc:mysql://localhost:3306/databasezfy?useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456 mybatis.config-locations=mybatis/mybatis-config.xml
mybatis.mapper-locations=mybatis/mapper/*.xml
这里配置主要包括两个部分,一个是mybatis自身的一些配置,例如基本类型的别名,是否支持缓存。第二个是指定mapper文件的位置。这个配置也可以通过 Java configuration来实现,由于篇幅的问题,我这里就不详述了,有兴趣的朋友可以自己实现一下。
配置完后,我们先编写mapper对应的接口:
@Service
public interface StudentMapper {
public int insertStudent(Student student);
}
该接口暂时只定义了一个方法,即插入操作。这是一个接口,并且和JPA类似,可以不用实现类。接下来我们编写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.demo.dao.StudentMapper">
<sql id="base_column">
id,name,age,schoolName,createTime
</sql> <insert id="insertStudent" parameterType="com.example.demo.entity.Student">
INSERT INTO
student(<include refid="base_column"/>)
VALUES
(#{id},#{name},#{age},#{schoolName},#{createTime})
</insert> </mapper>
好了,与MyBatis的集成也完成了,我们再测试一下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class)
public class StudentMapperTest { @Autowired
private StudentMapper studentMapper; @Test
public void testInsert() {
Student student = new Student();
student.setId("2");
student.setName("ls");
student.setAge("11");
student.setSchoolName("erzhong");
student.setCreateTime(new Date());
studentMapper.insertStudent(student);
}
}
测试一下,会发现报错
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.example.demo.dao.StudentMapperTest': Unsatisfied dependency expressed through field 'studentMapper'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.example.demo.dao.StudentMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:596) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:90) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
No qualifying bean of type 'com.example.demo.dao.StudentMapper' available,也就是说StudentMapperTest中依赖的StudentMapper unavaliable,即找不到StudentMapper类。所以在启动DemoApplication需要扫描StudentMapper类,加上@MapperScan注解扫描j将*Mapper扫描进来。@MapperScan注解
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
在测试一下:
插入成功:
捋一下StudentMapperTest整个插入操作执行的过程:
①加载Application这个类,扫描com.example.demo.dao包下的所有Mapper
②这样就可以通过@Autowired注解引入StudentMapper,执行insertStudent方
③通过配置文件application.properties中的mybatis.mapper-locations=mybatis/mapper/*.xml定位到*.xml文件,最后通过nameSpace即StudentMapper所在的路径(com.example.demo.dao.StudentMapper)和SQL语句中的id即方法名(insertStudent)唯一标识对应的SQL语句。
附上目录结构:
五、总结
本文演示Spring boot与JdbcTemplate、JPA以及MyBatis的集成,整体上来说配置都比较简单,以前做过相关配置的同学应该感觉比较明显,Spring boot确实在这方面给我们提供了很大的帮助。后续的文章中我们只会使用MyBatis这一种方式来进行数据库的操作。
参考资料:
Spring Boot(二):数据库操作的更多相关文章
- Spring Boot学习——数据库操作及事务管理
本文讲解使用Spring-Data-Jpa操作数据库. JPA定义了一系列对象持久化的标准. 一.在项目中使用Spring-Data-Jpa 1. 配置文件application.properties ...
- spring boot访问数据库
1. Spring JAP 基本使用说明: Spring boot 访问数据库基本上都是通过Spring JPA封装的Bean作为API的,Spring JPA 将访问数据库通过封装,只要你的类实现了 ...
- Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控
Spring Boot(二十):使用spring-boot-admin对spring-boot服务进行监控 Spring Boot Actuator提供了对单个Spring Boot的监控,信息包含: ...
- Spring Boot 二十个注解
Spring Boot 二十个注解 占据无力拥有的东西是一种悲哀. Cold on the outside passionate on the insede. 背景:Spring Boot 注解的强大 ...
- Spring Boot中快速操作Mongodb
Spring Boot中快速操作Mongodb 在Spring Boot中集成Mongodb非常简单,只需要加入Mongodb的Starter包即可,代码如下: <dependency> ...
- Spring Boot MyBatis 数据库集群访问实现
Spring Boot MyBatis 数据库集群访问实现 本示例主要介绍了Spring Boot程序方式实现数据库集群访问,读库轮询方式实现负载均衡.阅读本示例前,建议你有AOP编程基础.mybat ...
- spring boot由浅入深(二)spring boot基本命令及操作
一 spring常见注解 @RestController和@RequestMapping说明: @RestController.这被称为一个构造型(stereotype)注解.它为阅读代码的人们提供建 ...
- spring boot(二):web综合开发
上篇文章介绍了Spring boot初级教程:spring boot(一):入门篇,方便大家快速入门.了解实践Spring boot特性:本篇文章接着上篇内容继续为大家介绍spring boot的其它 ...
- Spring boot实现数据库读写分离
背景 数据库配置主从之后,如何在代码层面实现读写分离? 用户自定义设置数据库路由 Spring boot提供了AbstractRoutingDataSource根据用户定义的规则选择当前的数据库,这样 ...
随机推荐
- springboot~@Query到DTO对象
我们有时在进行开发过程中,使用jpa的@Query注解去选择多张表然后返回一个DTO对象,这个时候我们需要特殊处理一下,因为默认情况下,你的jpa代码是不认DTO对象的. 参考文章:https://s ...
- with open为什么会自动关闭文件流
操作文件我们通常需要手动关闭文件流,可是通过with open()的时候却可以自动关闭,这是为什么呢?其实这就是上下文管理器.我们来看一个例子 #!/usr/bin/env python # -*- ...
- Redis协议规范(RESP)
Redis 即 REmote Dictionary Server (远程字典服务): 而Redis的协议规范是 Redis Serialization Protocol (Redis序列化协议) 该协 ...
- Docker进阶之五:容器管理
容器管理 一.创建容器常用选项 docker container --help 指令 描述 资源限制指令 -i, --interactive 交互式 -m,--memory 容器可以使用的最大内存量 ...
- AppBoxFuture(五): 分布式文件存储-Store Everything
本来本篇是想介绍前端组件化开发用户界面,发现框架还未实现文件存储,原本计划是后续设计开发的,索性把计划提前,所以本篇将介绍基于Raft实现分布式的文件存储引擎. 一. 实现思路 既然是分布式存 ...
- .Net移动开发平台 ,基于VisualStudio的可视化开发——Smobiler平台入门教程
通过以下步骤,可以简单了解到如何下载Smobiler Designer(设计器).Client(客户端),以及如何通过设计器进行开发和调试移动应用,并在服务端部署.Cloud打包.访问您所开发的移动应 ...
- ssh远程连接vm 安装的ubuntu
准备工作 安装包 vmware 虚拟机.xshell.ubuntu 1.软件安装比较简单,这里不做介绍 2.选择虚拟机连接方式 3.添加虚拟IP 配置 编辑->虚拟网络编辑器->选择vm ...
- Java自增和自减操作符——++/--的那些事
1. 概述 自增操作符(++)和自减操作符(--)是对变量进行加1和减1的操作. 2.分类说明 ++和--是对变量进行自增1和自减1的简写操作符.许多编程任务中经常需要对变量加1或者减1,所以采用这两 ...
- 关于在Idea 创建Maven项目时,无法在source文件下创建servlet文件问题解决!
很简单:打开.iml文件,
- Android 字体适配方案
开发过程中,按照UI设计尺寸做好UI页面,当用户自定义自己的手机字体大小之后UI完全没法看了,这个时候就在想让app字体大小始终一致就好了 下面看一下,出现的问题和解决方案 做个简单的例子,先 ...