目录
[TOC]
Spring Data
连接MYSQL
- pom 配置
- application.yml 配置
- src\test\java\com\chasing\spring03data\Spring03DataApplicationTests.java 编辑测试类
pom引入
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
|
application.yml
1 2 3 4 5 6 7
| spring: datasource: username: 用户名 password: 密码 url: jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC driver-class-name: com.mysql.jdbc.Driver
|
src\test\java\com\chasing\spring03data\Spring03DataApplicationTests.java 编辑测试类
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.chasing.spring03data; 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 Spring03DataApplicationTests {
@Autowired DataSource dataSource;
@Test void contextLoads() throws SQLException { System.out.println(dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println(connection); connection.close(); }
}
|
CRUD
目录

增加Controller,结合网页get测试,直接操作数据
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
| package com.chasing.spring03data.Controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; import java.util.Map; @RestController public class JDBCcontroller { @Autowired JdbcTemplate jdbcTemplate; @GetMapping("/userList") public List<Map<String,Object>> userList (){ String sql = " select * from account order by id limit 10"; List<Map<String, Object>> list = jdbcTemplate.queryForList(sql); int i =1; for(Map map:list){ System.out.println("("+ i++ + ")==> "+ map); } return list; } @RequestMapping("/add") public String addAccount(){ String sql = "insert into account(id,name,memberid,customerId,createTime) values(1,'小米',101,400222000,'2018-08-27')"; jdbcTemplate.update(sql); return "addDONE"; } @RequestMapping("/update/{id}") public String updateAccount(@PathVariable("id") String id){ String sql = "update account set name=? where id="+id; Object newName = "小明"; jdbcTemplate.update(sql,newName); return "updateDONE"; } @RequestMapping("/del/{id}") public String delAccount(@PathVariable("id") String id){ String sql = "delete from account where id="+id; jdbcTemplate.update(sql); return "delDONE"; } }
|
使用Druid数据源
什么是druid,参考此博主文章
- pom
- application.yml
- 配置类
- 测试
pom引入
1 2 3 4 5 6 7 8 9 10 11 12
| <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
<dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.21</version> </dependency>
|
application.yml
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
| server: port: 8081
spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/boottest01?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC driver-class-name: com.mysql.cj.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5 minIdle: 5 maxActive: 20 maxWait: 60000 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true
filters: stat,wall,log4j maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
|
增加配置类com.chasing.spring03data.config.DruidConfig.java
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
| package com.chasing.spring03data.config;
import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource; import java.util.HashMap;
@Configuration public class DruidConfig { @ConfigurationProperties(prefix = "spring.datasource") @Bean public DataSource druidDataSource(){ return new DruidDataSource(); } @Bean public ServletRegistrationBean a(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
HashMap<String, String> initParameters = new HashMap<>(); initParameters.put("loginUsername","admin"); initParameters.put("loginPassword","123456"); initParameters.put("allow","");
bean.setInitParameters(initParameters); return bean; }
@Bean public FilterRegistrationBean webStartFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); HashMap<String, String> initParameters = new HashMap<>(); initParameters.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParameters); return bean; } }
|
访问http://localhost:8080/druid/login.html

显示druid自带的监控页面,输入在DruidConfig配置类里配置的用户名密码,就可以查看一些信息
整合Mybatis
配置
mybatis-spring-boot-starter
- pom引入
- application.yml 配置
- 编辑测试类
pom引入
(也可以直接创建springboot项目,然后导入web,jdbcapi,mysql模块。最后再引入mybatis的starter即可)
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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.chasing</groupId> <artifactId>springboot05-data</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot05-data</name> <description>Demo project for Spring Boot</description>
<properties> <java.version>1.8</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>2.3.0.RELEASE</spring-boot.version> </properties> 的 <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.0.RELEASE</version> <configuration> <mainClass>com.chasing.Springboot05DataApplication</mainClass> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
</project>
|
application.yml 配置
1 2 3 4 5 6
| spring: datasource: username: root password: xxxx url: jdbc:mysql://localhost:3306/数据库名?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver
|
com\chasing\Springboot05DataApplicationTests.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| package com.chasing; 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 Springboot05DataApplicationTests { @Autowired DataSource dataSource; @Test void contextLoads() throws SQLException { Connection connection = dataSource.getConnection(); System.out.println(connection); System.out.println(dataSource.getClass()); }
}
|
输出

CRUD
目录

表字段

src\main\resources\mybatis\mapper\AccountMapper.xml
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
| <?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.chasing.mapper.AccountMapper">
<select id="queryAccountList" resultType="Account" > SELECT * from account LIMIT 15 </select>
<select id="queryAccountById" resultType="Account" parameterType="int"> SELECT * from account WHERE id = #{id} </select>
<insert id="addAccount" parameterType="Account"> INSERT INTO account (id,name,memberId,customerId,createTime) VALUES (#{id},#{name},#{memberId},#{customerId},NOW()) </insert>
<update id="updateAccount" parameterType="Account"> UPDATE account set name=#{name},memberId=#{memberId},customerId=#{customerId} </update>
<delete id="delAccount" parameterType="int" > DELETE FROM account where id = #{id} </delete> </mapper>
|
com\chasing\mapper\AccountMapper.java
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.chasing.mapper;
import com.chasing.pojo.Account; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper @Repository public interface AccountMapper { List<Account> queryAccountList(); Account queryAccountById(int id); int addAccount(Account account); int updateAccount(Account account); int delAccount(int id);
}
|
src\main\java\com\chasing\controller\AccountController.java
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
| package com.chasing.controller; import com.chasing.mapper.AccountMapper; import com.chasing.pojo.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.List; @RestController public class AccountController { @Autowired private AccountMapper accountMapper;
@RequestMapping("/all") public List<Account> queryAccountList(){ List<Account> accounts = accountMapper.queryAccountList(); int count = 0; for (Account account:accounts ){ System.out.println(count++ + "==>"+ account); } return accounts; } @RequestMapping("/findByid/{id}") public Account queryAccountById(@PathVariable("id") int id){ Account account = accountMapper.queryAccountById(id); return account; } @RequestMapping("/add") public String add( ){ Integer i = accountMapper.addAccount(new Account(13, "19911110002", "333", "400222002", new Date().toString())); return i.toString(); } @RequestMapping("/update") public String update(){ Integer i = accountMapper.updateAccount(new Account(3,"新的","102","400222002",new Date().toString())); return i.toString(); } @RequestMapping("/del/{id}") public String del(@PathVariable("id") int id){ Integer i = accountMapper.delAccount(id); return i.toString(); }
}
|
Shiro
简介参考此博客:链接
Swagger
入门
pom引入
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
|
创建java

1 2 3 4 5 6 7 8 9
| package com.chasing.config;
import org.springframework.context.annotation.Configuration; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration @EnableSwagger2 public class SwaggerConfig { }
|
访问
http://localhost:8080/swagger-ui.html
配置
在上面的java文件中增加配置
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
| package com.chasing.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList;
@Configuration @EnableSwagger2 public class SwaggerConfig {
@Bean public Docket docket(){ return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()); } public ApiInfo apiInfo(){ Contact DEFAULT_CONTACT = new Contact("作者名", "https://xxxx", "xxxx@qq.com"); return new ApiInfo( "我的swagger API文档", "Api Documentation", "1.0", "urn:tos", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList() ); } }
|
运行后:

questions
@Validated的作用
@ConfigurationProperties 与 @PropertySource的区别