- spring boot 整合mysql和mybatis
整合mysql 和shiro
mysql:
配置数据源
spring:
datasource:
url: jdbc:mysql://localhost:3306/hwx?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456
filters: wall,mergeStat
type: com.alibaba.druid.pool.DruidDataSource
druid:
filters: stat,wall
stat-view-servlet:
enabled: true
login-username: hwxadmin
login-password: hwx001125
web-stat-filter:
enabled: true
urlpattern: /*
exclusions: '*.js,*.jgp,*.png,*.gif,*.css,*.ico,/druid/*'
filter:
slow-sql-millis: 1000
logslowsql: true
enabled: true
配置mybatis时,需要配置xml文件的扫描位置
只需要在yml文件中配置
mybatis-plus:
mapper-locations: classpath:myBatis/*.xml
顶格即可
mybatis
配置mybatis的xml时,
插入用select标签,返回值为Integer,
查询用select标签,返回值为实体类
配置数据库和实体类的关系可以用resultMap标签
property为数据库返回的字段名,column为相对应的实体类的字段名
xml的映射接口mapper需要继承 BaseMapper< shirouser > ,这个shirouser为实体类
spring boot
1:
mapper接口需要@mapper注解否则会报找不到bean的错误,如果标注了注解还是报错,可以在启动类上添加一个扫描mapper的注解@MapperScan(basePackages = “com.demo.mapper”)
2:
server的impl需要添加@service的注解
3:
自己定义的工具类添加@Component注解
需要在工具类中使用其他工具类时,不能自己new,需要在wenconfig中添加@bean ,将其加入到spring ioc 或者
private static bookmainmapper bookmainmapper;
private static bookmainService bookmainService;
@Autowired
public void setBookmainmapper(bookmainmapper bookmainmapper){
bookdate.bookmainmapper=bookmainmapper;
}
@Autowired
public void setBookmainService(bookmainService bookmainService){
bookdate.bookmainService=bookmainService;
}
public bookdate() {
}
4:
配置类记得加上@Configuration注解
shiro:
配置shiroconfig:
自定义realm继承AuthorizingRealm接口,然后在webconfig中@bean,将其加入到spring ioc
然后将realm加入到DefaultWebSecurityManager,再将,DefaultWebSecurityManager加入到ShiroFilterFactoryBean即可
自定义的realm继承AuthorizingRealm,实现两个方法
1:
doGetAuthorizationInfo
这个方法用来做权限认证
2:
doGetAuthenticationInfo
这个方法用来做登录认证