创建项目
选择MySQL+JDBC+Web
链接数据库
1 2 3 4 5 6
| spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/jdbc driver-class-name: com.mysql.jdbc.Driver
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package com.wsx.study.springboot.demo;
import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException;
@SpringBootTest class DemoApplicationTests {
@Autowired DataSource dataSource;
@Test void contextLoads() throws SQLException { System.out.println(dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); } }
|
数据源自动配置原理
DataSourceConfiguration,根据配置创建数据源,默认使用Tomcat连接池
SpringBoot支持很多数据源类型,甚至可以自定义,利用反射绑定数据源,并绑定相关属性
配置druid
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.22</version> </dependency>
|
然后再application.yml中配置这个, 就可以用localhost:8080/druid/来访问来
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| spring: datasource: druid: username: root password: 123456 url: jdbc:mysql://127.0.0.1:3306/debug?serverTimezone=GMT%2B8&characterEncoding=UTF-8&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 5 min-idle: 5 max-active: 20 max-wait: 60000 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 300000 validation-query: SELECT 'x' test-while-idle: true test-on-borrow: false test-on-return: false poolPreparedStatements: true maxPoolPreparedStatementPerConnectionSize: 20 filters: stat,wall web-stat-filter: enabled: true url-pattern: /* exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*" session-stat-enable: false session-stat-max-count: 1000 principal-cookie-name: admin principal-session-name: admin profile-enable: true stat-view-servlet: enabled: true url-pattern: /druid/* reset-enable: true login-username: admin login-password: 123456 allow: deny:
|
整合MyBatis
建表+逆向工程
SpringBoot就自动帮我们把Mapper放到IOC中了
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| package com.wsx.demo.dao;
import com.wsx.demo.pojo.Table1; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;
import java.util.List;
@Mapper public interface Table1Mapper { @Select("select * from table1") public List<Table1> selectAll(); }
|
当然这里也支持批量导入
1 2 3 4 5 6 7 8 9 10
| @SpringBootApplication @MapperScan("com.wsx.demo.dao") public class DemoApplication {
public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }
}
|
整合JPA
最后是最强的。。。
新建项目的时候选择JPA
JPA你甚至不用自己写sql?好强大
1 2 3 4 5 6 7 8 9 10
| spring: datasource: url: jdbc:mysql://localhost/debug username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.Driver jpa: hibernate: ddl-auto: update show-sql: true
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| package com.wsx.demo.entity;
import lombok.Data;
import javax.persistence.*;
@Data @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) Integer id; String lastName; String email; }
|
1 2 3 4 5 6 7 8
| package com.wsx.demo.repository;
import com.wsx.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<User, Integer> { }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| package com.wsx.demo.controller;
import com.wsx.demo.entity.User; import com.wsx.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Optional;
@Controller public class UserController { @Autowired UserRepository userRepository;
@GetMapping("/user/select/{id}") @ResponseBody public Optional<User> selectUser(@PathVariable("id") Integer id) { return userRepository.findById(id); }
@GetMapping("/user/insert") @ResponseBody public User insertUser(User user) { return userRepository.save(user); } }
|
SpringBoot整合Druid
最后更新时间:
这里可以写作者留言,标签和 hexo 中所有变量及辅助函数等均可调用,示例:
<%- page.permalink.replace(/index\.html$/, '') %>