代码已提交至Github,有兴趣的同学可以下载来看看:https://github.com/ylw-github/SpringBoot-DB-Demo
本文目录结构:
l____1. SpringBoot整合JdbcTemplate
1. SpringBoot整合JdbcTemplate
1.添加maven依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> </parent> <dependencies> <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.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.application.properties新增配置
spring.datasource.url=jdbc:mysql://localhost:3306/test spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver
下图是我的test数据库中的t_user表
3.UserService类及实现
//1.UserService接口 public interface UserService { public void createUser(String uuid, String name, Integer age); } //2.UserService实现类 @Service public class UserServiceImpl implements UserService { @Autowired private JdbcTemplate jdbcTemplate; public void createUser(String uuid, String name, Integer age) { System.out.println("插入人员 name -> " + name + " age ->" + age); jdbcTemplate.update("insert into t_user(uuid,name,age) values(?,?,?);", uuid, name, age); } }
4.App程序启动类
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com.ylw") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5.springboot单元测试,单元测试之前,需要先添加单元测试依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
单元测试代码:
import com.ylw.springboot.App; import com.ylw.springboot.service.UserService; import org.junit.Test; 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.UUID; @RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class JDBCTest { @Autowired UserService userService; @Test public void testJDBCTemplate() { userService.createUser(UUID.randomUUID().toString(),"Dumas", 18); } }
6.运行后,会发现数据库中,成功插入了一条数据:
2. SpringBoot整合MyBatis
1.mave依赖,
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.配置文件的引入(同JdbcTemplate一样)
3.Mapper代码
package com.ylw.springboot.mapper; import com.ylw.springboot.bean.User; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; public interface UserMapper { @Select("SELECT * FROM t_user WHERE NAME = #{name}") User findByName(@Param("name") String name); @Insert("INSERT INTO t_user(uuid,name, age) VALUES(#{uuid},#{name}, #{age})") int insert(@Param("uuid") String uuid, @Param("name") String name, @Param("age") Integer age); }
4.设置启动方式(注意要配置扫描Mapper的目录)
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; @ComponentScan("com.ylw") @MapperScan("com.ylw.springboot.mapper") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5.单元测试
@RunWith(SpringRunner.class) @SpringBootTest(classes = App.class) public class MyBatisTest { @Autowired UserMapper userMapper; @Test public void testQuery() { User user = userMapper.findByName("Dumas"); System.out.println(user.toString()); } }
运行结果:
3. SpringBoot整合SpringJPA
1.maven依赖文件
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.2.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.21</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2.创建JPA实体:
package com.ylw.springboot.bean; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; @Entity(name = "t_user") public class JPAUser { @Id @Column private String uuid; @Column private String name; @Column private Integer age; @Column private String sex; @Column private String job; public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } 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; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } @Override public String toString() { return "JPAUser{" + "uuid='" + uuid + '\'' + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + ", job='" + job + '\'' + '}'; } }
3.创建UserDao:
package com.ylw.springboot.dao; import com.ylw.springboot.bean.JPAUser; import org.springframework.data.jpa.repository.JpaRepository; public interface UserDao extends JpaRepository<JPAUser, String> { }
4.设置启动类:
package com.ylw.springboot; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.context.annotation.ComponentScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; @ComponentScan("com.ylw.springboot") @EnableJpaRepositories("com.ylw.springboot.dao") @EntityScan("com.ylw.springboot.bean") @EnableAutoConfiguration public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
5.单元测试:
import com.ylw.springboot.App; import com.ylw.springboot.bean.JPAUser; import com.ylw.springboot.dao.UserDao; import org.junit.Test; 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(classes = App.class) public class JPATest { @Autowired UserDao userDao; @Test public void test() { List<JPAUser> users = userDao.findAll(); if (users == null || users.size() == 0) { return; } for (JPAUser user : users) { System.out.println(user.toString()); } } }
运行结果:
4. SpringBoot整合多数据源
参考:https://www.jianshu.com/p/099c0850ba16
总结