<!--引入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>