spring boot整合mybatis方式一

时间:2022-12-06 13:18:50

方式一:

导入maven依赖:  

<!--web依赖配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--引入mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--工具类-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--guawa工具类-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>27.0.1-jre</version>
</dependency>

配置application.properties:
#druid
spring.druid.url=jdbc:mysql:///mysql04?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT
spring.druid.driverClassName=com.mysql.jdbc.Driver
spring.druid.username=root
spring.druid.password=root
#最大链接数
spring.druid.maxActive=30
#最小链接数
spring.druid.minIdle=5
#获得链接的最大等待时间
spring.druid.maxWait=10000 #指定mybatis配置文件地址
mybatis.config-location=classpath:mybatis/mybatis-config.xml #端口
server.port=8888

配置DruidConfig配置类(包含sql检测:显示慢sql):

@Configuration
public class DruidConfig { @Bean
@ConfigurationProperties(prefix = "spring.druid")
public DruidDataSource druidDataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
return dataSource;
}
@Bean
public StatFilter statFilter(){
StatFilter filter = new StatFilter();
filter.setLogSlowSql(true);
filter.setMergeSql(true);
filter.setSlowSqlMillis(5);
return filter;
} @Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
} }

配置mybatis核心配置文件(主要配置别名与映射xml文件):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration> <settings>
<!--关闭缓存,多机环境 数据更新 不会刷新缓存-->
<setting name="cacheEnabled" value="false"/>
<!--开启驼峰匹配-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--允许主键生成-->
<setting name="useGeneratedKeys" value="true"/>
<!--配置默认的执行器。SIMPLE 就是普通的执行器;
REUSE 执行器会重用预处理语句(prepared statements);
BATCH 执行器将重用语句并执行批量更新。
请自行百度,这部分内容不会详细介绍,涉及到架构设计,跟我们暂时还不沾边
https://blog.csdn.net/qingtian211/article/details/81838042
-->
<setting name="defaultExecutorType" value="REUSE"/>
<setting name="defaultStatementTimeout" value="600"/>
</settings> <typeAliases>
<package name="com.springbootmybatis.procedure.entity"/>
</typeAliases> <mappers>
<mapper resource="mapper/OrderMapper.xml"/>
</mappers>
</configuration>

配置xml文件,与mapper层一一对应:

<?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.springbootmybatis.procedure.mapper.OrderMapper">
<update id="cancelOrder" parameterType="string" statementType="CALLABLE">
call cancel_order(#{orderId,jdbcType=VARCHAR,mode=IN})
</update>
<select id="findAll" resultType="order">
select * from xmcc_order
</select>
</mapper>

再之后就是配置实体类与mapper层接口,此处省略。。。

springboot整合junit测试(两个注解):

@RunWith(SpringRunner.class)
@SpringBootTest
public class ProcedureApplicationTests { @Autowired
private OrderMapper orderMapper; @Test
public void contextLoads() {
List<Order> orders = orderMapper.findAll();
orders.stream().forEach(System.out::println);
}
}

最后附一份GitHub项目仓库地址:

https://github.com/ycheneye/procedure