<!--引入mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.2</version> </dependency> <!--引入mysql驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <!-- alibaba的druid数据库链接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <!--引入lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.10</version> </dependency>
能够看到mybatis的starter中导入了spring-boot-starter-jdbc,所以这里不须要再次导入jdbc依赖css
spring: # 配置数据源 datasource: # 数据库url url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8&useSSL=true # 用户名 username: root # 密码 password: root # 驱动类 driver-class-name: com.mysql.cj.jdbc.Driver # 配置数据源类型,这里选择druid type: com.alibaba.druid.pool.DruidDataSource # druid相关配置 druid: #初始化大小 initialSize: 5 #最小值 minIdle: 5 #最大值 maxActive: 20 #最大等待时间,配置获取链接等待超时,时间单位都是毫秒ms maxWait: 60000 #配置间隔多久才进行一次检测,检测须要关闭的空闲链接 timeBetweenEvictionRunsMillis: 60000 #配置一个链接在池中最小生存的时间 minEvictableIdleTimeMillis: 300000 validationQuery: SELECT 1 FROM DUAL testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true # 配置监控统计拦截的filters,去掉后监控界面sql没法统计, #'wall'用于防火墙,SpringBoot中没有log4j,我改为了log4j2 filters: stat,wall,log4j2 #最大PSCache链接 maxPoolPreparedStatementPerConnectionSize: 20 useGlobalDataSourceStat: true # 经过connectProperties属性来打开mergeSql功能;慢SQL记录 connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500 # 配置StatFilter web-stat-filter: #默认为false,设置为true启动 enabled: true url-pattern: "/*" exclusions: "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*" #配置StatViewServlet stat-view-servlet: url-pattern: "/druid/*" #容许那些ip allow: 127.0.0.1 login-username: admin login-password: 123456 # 禁止那些ip deny: 192.168.1.102 #是否能够重置 reset-enable: true #启用 enabled: true
package com.cheng.cache.pojo; import lombok.Getter; import lombok.Setter; /** * @author cheng * @version 1.0 * @date 2020/3/21 */ @Getter @Setter public class Student { private Integer id; private String name; private Integer age; }
# 配置mybatis mybatis: # pojo类所在路劲 type-aliases-package: com.cheng.cache.pojo configuration: # 配置项:开启下划线到驼峰的自动转换. 做用:将数据库字段根据驼峰规则自动注入到对象属性。 map-underscore-to-camel-case: true
@MapperScan("com.cheng.cache.mapper")
public interface StudentMapper { @Options(useGeneratedKeys = true, keyProperty = "id") @Insert("insert into student(name, age) values(#{name}, #{age})") public int insert(Student student); @Delete("delete from student where id = #{id}") public int delete(Integer id); @Select("select * from student where id = #{id}") public Student select(Integer id); @Update("update student set name = #{name},age = #{age} where id = #{id}") public int update(Student student); }
# 配置mybatis mybatis: # 映射文件所在路径 mapper-locations: classpath:mybatis/mapper/*.xml # pojo类所在路径 type-aliases-package: com.cheng.cache.pojo configuration: #配置项:开启下划线到驼峰的自动转换. 做用:将数据库字段根据驼峰规则自动注入到对象属性。 map-underscore-to-camel-case: true
<?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.cheng.cache.xmlmapper.XmlStudentMapper"> <!--resultMap是Mybatis最强大的元素,它能够将查询到的复杂数据(好比查询到几个表中数据)映射到一个结果集当中--> <resultMap id="baseMap" type="com.cheng.cache.pojo.Student"> <!--column不作限制,能够为任意表的字段,而property须为type 定义的pojo属性--> <id column="id" property="id" jdbcType="INTEGER"></id> <result column="name" property="name" jdbcType="VARCHAR"></result> <result column="age" property="age" jdbcType="INTEGER"></result> </resultMap> <!--public Student select(Integer id);--> <select id="select" parameterType="java.lang.Integer" resultMap="baseMap"> select * from student where id = #{id} </select> <!--public int insert(Student student);--> <insert id="insert" parameterType="com.cheng.cache.pojo.Student" useGeneratedKeys="true" keyProperty="id"> insert into student(name, age) values(#{name}, #{age}) </insert> <!--public int delete(Integer id);--> <delete id="delete" parameterType="java.lang.Integer"> delete from student where id = #{id} </delete> <!--public int update(Student student);--> <update id="update" parameterType="com.cheng.cache.pojo.Student"> update student set name = #{name}, age=#{age} where id = #{id} </update> </mapper>