引入数据库连接池技术包装了jdbc(避免原生jdbc频繁建立连接,达到连接复用的效果)
1、具体数据库驱动需要自己引入(这里使用自定义版本,而不是parent控制版本)
2、使用的哪种第三方数据库连接池技术需要自己引入
3、SpringBoot有第三方starter:mybatis-spring-boot-starter
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.46version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.1.17version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.1.4version>
dependency>
spring:
#数据源连接数据库基本属性配置
datasource:
url: jdbc:mysql:///spring
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
由于druid还提供了很好的监控功能,我们可以对整个项目进行监控
datasource:
......
druid:
#基本配置
initial-size: 5
max-active: 20
max-wait: 1000 #单位ms
pool-prepared-statements: false #mysql下不缓存preparedStatement
#监控配置
#开启druid监控视图Servlet
stat-view-servlet:
enabled: true
login-username: admin
login-password: admin
#web监控
web-stat-filter:
enabled: true
url-pattern: /*
session-stat-enable: true
#对Spring监控
aop-patterns: com.neuq.admin.* #监控该包下的Spring
#对sql的监控与设置(防止sql注入等非法操作)
filter:
#sql监控
stat:
enabled: true
log-slow-sql: true #监控慢查询
slow-sql-millis: 1000
#sql防火墙
wall:
enabled: true
config:
drop-table-allow: false #拦截删表操作
druid监控视图Servlet(“/druid/index.html”)
配置Mybatis实现驼峰命名规范转换!!!
#mtbatis配置
mybatis:
configuration:
map-underscore-to-camel-case: true
@Mapper:告诉Mybatis这是需要映射实现的接口
@Repository:持久层组件注入IOC注释 (可以不用)
@操作方式(sql语句)【参数类型、返回类型都不用写,直接反射获取】
@Mapper
@Repository
public interface UserDao {
//注册
@Insert("insert into admin(username,password,full_name,address,email,city,gender) " +
"values (#{username},#{password},#{fullName},#{address},#{email},#{city},#{gender})")
@Options(useGeneratedKeys = true, keyProperty = "userId", keyColumn = "user_id")
void insertUser(User user);
//登录
@Select("select * from admin where username=#{username} and password=#{password}")
User queryUser(User user);
//验证用户名
@Select("select * from admin where username=#{username}")
User queryUserByName(User user);
}
结束了,对,就结束了!!
@Options(…)属性:
@Configuration
@EnableTransactionManagement
public class MyBatisConfig {
}
注意:是业务层接口!!!
//支付
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.REPEATABLE_READ,timeout = 36000,rollbackFor = Exception.class)
public boolean pay(User from, User to, BigDecimal money);
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>1.2.5version>
dependency>
#pagehelper配置
pagehelper:
helper-dialect: mysql #支持mysql
reasonable: true #合理化 小于0查询第一页 大于最大 查询最后一页
support-methods-arguments: true #是否通过mapper接口传递参数
params: count=countSql #总页数为countSql 目前不知道怎么用
注意:
public String dynamicPage(Integer pageNum, Integer pageSize, Model model){
//启用PageHelper分页功能 统一拦截sql为其提供分页功能
Page<Object> page = PageHelper.startPage(pagrNum, pageSize);
List<User> users = userService.queryUsers();
//获得总数据量
long total = page.getTotal();
//获得总页数
int pages = page.getPages();
model.addAttribute("total",total);
model.addAttribute("pages",pages);
model.addAttribute("users",users);
return "table/dynamic_table";
}
1、在resources资源文件夹下创建持久层接口的xml映射文件的存放位置
2、属性配置文件中告诉Mybatis持久层接口的xml映射文件的路径
#mtbatis配置
mybatis:
mapper-locations: classpath:daoMapper/*.xml
@Mapper:告诉Mybatis这是需要映射实现的接口
@Repository:持久层组件注入IOC注释
@Mapper
@Repository
public interface UserDao {
//注册
void insertUser(User user);
//登录
User queryUser(User user);
//验证用户名
User queryUserByName(User user);
}
1、和原生Mybatis一样实现
DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.neuq.admin.dao.UserDao">
<insert id="insertUser">
insert into admin(username,password,full_name,address,email,city,gender)
values (#{username},#{password},#{fullName},#{address},#{email},#{city},#{gender})
insert>
<select id="queryUser" resultType="com.neuq.admin.domain.User">
select *
from admin
where username=#{username} and password=#{password}
select>
<select id="queryUserByName" resultType="com.neuq.admin.domain.User">
select *
from admin
where username=#{username}
select>
mapper>