SpringBoot整合JDBC
配置
spring :
datasource :
username : 用户名
password : 密码
url : jdbc: mysql: //localhost: 3306/test? useUnicode=true&characterEncoding=utf- 8
driver-class-name : com.mysql.cj.jdbc.Driver
@Autowired
数据库Druid
spring :
datasource :
username : root
password : root
url : jdbc: mysql: //localhost: 3306/test? useUnicode=true&characterEncoding=utf- 8
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
配置/config/DruidConfig.java 在localhost:3306中可以访问到项目的sql访问情况,后台页面
package com. example. springbootdata. config ;
import com. alibaba. druid. pool. DruidDataSource ;
import com. alibaba. druid. support. http. StatViewServlet ;
import org. springframework. boot. context. properties. ConfigurationProperties ;
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 statViewServlet ( ) {
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;
}
}
整合Mybatis框架
配置pom.xml,手动添加mybatis依赖(包含了mysql和jdbc的驱动)
< dependency>
< groupId> tk.mybatis</ groupId>
< artifactId> mapper-spring-boot-starter</ artifactId>
< version> 2.0.2</ version>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-data-jdbc</ artifactId>
</ dependency>
< 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>
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: --包名--
创建/mapper/UserMapper接口以及pojo类User
@Mapper
@Repository
public interface UserMapper {
List < User > queryUserList ( ) ;
User queryUser ( int id) ;
int addUser ( User user) ;
int updateUser ( User user) ;
int deleteUser ( int id) ;
}
编写/resources/mybatis/mapper/UserMapper.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.springbootdata.Mapper.
AccountMapper" >
< select id = " queryAccountList" resultType = " Account" >
select * from user
</ select>
< insert id = " addAccount" parameterType = " Account" >
insert into user values(#{id},#{name},#{password},#{age})
</ insert>
< update id = " updateAccount" parameterType = " Account" >
update user set name = #{name},password = #{password} where id = #{id}
</ update>
< delete id = " deleteAccount" parameterType = " int" >
delete from user where id = #{id}
</ delete>
</ mapper>
@RestController
public class AccountController {
@Autowired
private AccountMapper accountMapper;
@GetMapping ( "/queryUserList" )
public List < Account > queryUserList ( ) {
List < Account > accounts = accountMapper. queryUserList ( ) ;
for ( Account account: accounts)
{
System . out. println ( account) ;
}
return accounts;
}
@GetMapping ( "/addUser" )
public String addUser ( ) {
accountMapper. addUser ( new Account ( 2 , "李应红" , "123456" , 20 ) ) ;
return "ok" ;
}
}
Spring Security(认证和授权)
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-security</ artifactId>
</ dependency>
新建config/SecurityConfig.class
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure ( HttpSecurity http) throws Exception {
super . configure ( http) ;
}
}
@Override
protected void configure ( HttpSecurity http) throws Exception {
http. authorizeRequests ( )
. antMatchers ( "/" ) . permitAll ( )
. antMatchers ( "/level1/**" ) . hasAnyRole ( "vip1" )
. antMatchers ( "/level2/**" ) . hasAnyRole ( "vip2" )
. antMatchers ( "/level3/**" ) . hasAnyRole ( "vip3" ) ;
http. formLogin ( ) . loginPage ( "/toLogin" ) . usernameParameter ( "自定义用户名name" ) . passwordParameter ( "自定义密码name" ) . loginProcessingUrl ( "/login" ) ;
http. csrf ( ) . disable ( ) ;
http. logout ( ) . logoutSuccessUrl ( "/" ) ;
}
< a class = " item" th: href= " @{/toLogin}" >
< i class = " address card icon" > </ i> 登录
</ a>
< a class = " item" th: href= " @{/logout}" >
< i class = " address card icon" > </ i> 注销
</ a>
@Override
protected void configure ( AuthenticationManagerBuilder auth) throws Exception {
auth. inMemoryAuthentication ( ) . passwordEncoder ( new BCryptPasswordEncoder ( ) )
. withUser ( "langzhizhen" ) . password ( new BCryptPasswordEncoder ( ) . encode ( "123456" ) ) . roles ( "vip2" , "vip3" )
. and ( )
. withUser ( "admin" ) . password ( new BCryptPasswordEncoder ( ) . encode ( "123456" ) ) . roles ( "vip1" , "vip2" , "vip3" )
. and ( )
. withUser ( "guest" ) . password ( new BCryptPasswordEncoder ( ) . encode ( "123456" ) ) . roles ( "vip1" ) ;
}
//需要现在.html文件里声明扩展包来实现提示语法
//使用sec:autorize属性来判断
< html lang = " en" xmlns: th= " http://www.thymeleaf.org"
xmlns: sec= " http://www.thymeleaf.org/thymeleaf-extras-springsecurity5" >
< div sec: authorize= " !isAuthenticated()" >
< a class = " item" th: href= " @{/toLogin}" >
< i class = " address card icon" > </ i> 登录
</ a>
</ div>
< div sec: authorize= " isAuthenticated()" >
< a class = " item" >
用户名: < span sec: authentication= " name" > </ span>
角色: < span sec: authentication= " principal.authorities" > </ span>
</ a>
</ div>
< div sec: authorize= " isAuthenticated()" >
< a class = " item" th: href= " @{/logout}" >
< i class = " address card icon" > </ i> 注销
</ a>
</ div>
< div class = "column" sec: authorize= "hasRole('vip1')" > < / div>
开启记住我功能和自定义首页和快捷显示不同页面的请求
http. rememberMe ( ) . rememberMeParameter ( "remember" ) ;
@RequestMapping ( "/level1/{id}" )
public String level1 ( @PathVariable ( "id" ) int id)
{
return "views/level1/" + id;
}
@RequestMapping ( "/level2/{id}" )
public String level2 ( @PathVariable ( "id" ) int id)
{
return "views/level2/" + id;
}
@RequestMapping ( "/level3/{id}" )
public String level3 ( @PathVariable ( "id" ) int id)
{
return "views/level3/" + id;
}