赞
踩
CREATE TABLE `user` (
`id` bigint(20) NOT NULL,
`name` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;

<dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.23</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency>
spring: datasource: druid: url: jdbc:mysql://localhost:3306/bg-learnsp?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&useSSL=false # 连接数据库的url username: root # 连接数据库的用户名 password: root # 连接数据库的密码。 driver-class-name: com.mysql.cj.jdbc.Driver # 这一项可配可不配,如果不配置druid会根据url自动识别dbType,然后选择相应的driverClassName initial-size: 5 # 初始化时建立物理连接的个数。初始化发生在显示调用init方法,或者第一次getConnection时 max-active: 10 # 最大连接池数量 max-wait: 60000 # 获取连接时最大等待时间,单位毫秒。配置了maxWait之后,缺省启用公平锁,并发效率会有所下降,如果需要可以通过配置useUnfairLock属性为true使用非公平锁。 min-idle: 1 # 最小连接池数量 time-between-eviction-runs-millis: 60000 # 有两个含义: 1) Destroy线程会检测连接的间隔时间,如果连接空闲时间大于等于minEvictableIdleTimeMillis则关闭物理连接。 2) testWhileIdle的判断依据 test-while-idle: true # 建议配置为true,不影响性能,并且保证安全性。申请连接的时候检测,如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效。 min-evictable-idle-time-millis: 30000 # 连接保持空闲而不被驱逐的最小时间 test-on-borrow: true # 申请连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 test-on-return: false # 归还连接时执行validationQuery检测连接是否有效,做了这个配置会降低性能 pool-prepared-statements: true # 是否缓存preparedStatement,也就是PSCache。PSCache对支持游标的数据库性能提升巨大,比如说oracle。在mysql下建议关闭。 max-open-prepared-statements: 10 validation-query: select 1 # 用来检测连接是否有效的sql,要求是一个查询语句,常用select ‘x’。如果validationQuery为null,testOnBorrow、testOnReturn、testWhileIdle都不会起作用 validation-query-timeout: 50 # 单位:秒,检测连接是否有效的超时时间。底层调用jdbc Statement对象的void setQueryTimeout(int seconds)方法 filter: start # 属性类型是字符串,通过别名的方式配置扩展插件,常用的插件有: # 监控统计用的filter:stat # 日志用的filter:log4j # 防御sql注入的filter:wall # 增加日志记录 logging: level: root: info com.learn.mapper: debug mybatis: mapper-locations: classpath:mapper/**/*.xml # 指定mapper文件的位置
Druid连接池是阿里巴巴开源的数据库连接池项目。Druid连接池为监控而生,内置强大的监控功能,监控特性不影响性能。功能强大,能防SQL注入,内置Loging能诊断Hack应用行为。上面常用的参数已经详细介绍了含义,如果想要了解更多配置可以访问DruidDataSource配置属性列表,想要了解DruidDataSource更多信息可以访问DruidDataSource官网。
@Data
public class UserEntity {
private Long id;
private String name;
private Integer age;
}
public interface UserMapper { /** * 通过id查询用户信息 * * @param id * @return */ UserEntity findUserById(Long id); /** * 插入用户信息 * * @param user * @return */ int insert(UserEntity user); }
@SpringBootApplication
@MapperScan("com.learn.mapper")
public class SpLearnMybatis02Application {
public static void main(String[] args) {
SpringApplication.run(SpLearnMybatis02Application.class, args);
}
}
<?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.learn.mapper.UserMapper">
<select id="findUserById" parameterType="long" resultType="com.learn.entity.UserEntity">
SELECT * FROM USER WHERE ID = #{ID}
</select>
<insert id="insert">
INSERT INTO USER(ID,NAME, AGE) VALUES(#{id},#{name}, #{age})
</insert>
</mapper>
mybatis:
mapper-locations: classpath:mapper/**/*.xml # 指定mapper文件的位置
10.编写测试用例:
@SpringBootTest class SpLearnMybatis02ApplicationTests { @Autowired private UserMapper userMapper; @Test void testUserMapper() { UserEntity userEntity = new UserEntity(); userEntity.setId(9999L); userEntity.setName("阿里巴巴"); userEntity.setAge(85); //保存用户 userMapper.insert(userEntity); //查询用户 UserEntity user = userMapper.findUserById(9999L); System.out.println(user); } }
测试结果:
UserEntity(id=9999, name=阿里巴巴, age=85)
到这里,使用spring boot整合mybatis使用xml文件的方式,配合使用DruidDataSource数据源访问MySQL数据库的小案例就搭建好了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。