在application.properties文件中进行配置
spring.datasource.url=jdbc:mysql://lacalhost:3306/freeroom spring.datasource.username=root spring.datasource.password=123 # 最大等待连接中的数量 0无限制 spring.datasource.tomcat.max-idle=10 # 最大连接活动数 spring.datasource.tomcat.max-active=50 # 数据库连接池初始化连接数 spring.datasource.tomcat.initial-size=5JdbcTemplate模版是Spring框架提供的,在实际的工作使用较少,基本上都在使用Hibernate和MyBatis
JPA(Java Persistence API ,java持久化API),在spring boot中JPA是依靠Hibernate才得以实现的。
添加jpa的依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>此接口不用实现定义就行了,之后会自动注入
在对数据进行操作的时候,使用自动注入的jpaUserRespository实例进行操作。
最后需要将接口扫描到Spring IoC容器中。同时还需要将实体类(Entity Bean)注册给JPA。使用@EnableJpaRespositories和@EntityScan两个注解来实现
@SpringBootApplication @EnableJpaRepositories @EntityScan public class FreeRomeApplication { public static void main(String[] args) { System.out.println("开始"); SpringApplication.run(FreeRomeApplication.class,args); System.out.println("结束"); } }同样也是在application.properties
# 选择Hibernate数据定义语言(DDl)策略为update spring.jpa.hibernate.ddl-auto=update为了更加灵活的查询,可以使用JPA查询语言(JPQL)。使用方式是@Query标识语句。
在先前的JpaUserRespository接口中添加方法 例:
public User findByName(String name);等效于
select u from User u where u.name = ?1
也就是说findByName 会根据相应表里的字段name来select 参数name就是要查询的内容
现目前java持久层最为主流的技术应该就是MyBatis,它更简单、灵活、易用。 参考教程
MyBatis是基于SqlSessionFactory构建的框架
添加依赖第一步也是要先引入依赖mybatis-spring-boot-starter和mysql-connector-java 可以在https://mvnrepository.com/找mybatis-spring-boot-starter的最新版本
配置文件 server.port=8333 spring.datasource.url=jdbc:mysql://127.0.0.1:3306/erp?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT+8 spring.datasource.username=root spring.datasource.password=153963 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver 创建Bean public class User{ private int id; private String name; priavte int age; private double money; /** setter,getter and toString **/ }注意如果@Autowired UserDao时提示找不到可以尝试以下方法: 方法1:在mapper文件上加@Repository注解,这是从spring2.0新增的一个注解,用于简化 Spring 的开发,实现数据访问 方法2:在mapper文件上加@Component注解,把普通pojo实例化到spring容器中
Controller层 @RestController @RequestMapping("/user") public class UserController{ @Autowired private UserService userService' @RequestMapping("/query") public User testQuery(){ return userService.selectUserByName("Daisy"); } @RequestMapping("/insert") public List<User> testInsert() { userService.insertService(); return userService.selectAllUser(); } @RequestMapping("/changemoney") public List<User> testchangemoney() { userService.changemoney(); return userService.selectAllUser(); } @RequestMapping("/delete") public String testDelete() { userService.deleteService(3); return "OK"; } ] 启动类 //此注解表示SpringBoot启动类 @SpringBootApplication // 此注解表示动态扫描DAO接口所在包,实际上不加下面这条语句也可以找到 @MapperScan("top.snailclimb.dao") public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } }