<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>com.oracle.ojdbc</groupId>
<artifactId>ojdbc8</artifactId>
<scope>runtime</scope>
package com.example.mybatis.mapper;
import com.example.mybatis.entity.Dept;
import java.util.List;
//@Mapper
public interface DeptMapper {
List<Dept> getAll();
Dept getOne(Integer deptno);
void insert(Dept dept);
void update(Dept dept);
void delete(Integer deptno);
}
package com.example.mybatis.service;
import com.example.mybatis.entity.Dept;
import com.example.mybatis.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class DeptService {
@Autowired
private DeptMapper deptMapper;
public List<Dept> getAll(){
return deptMapper.getAll();
}
public Dept getOne(Integer deptno){
return deptMapper.getOne(deptno);
}
public void insert(Dept dept){
deptMapper.insert(dept);
}
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public void update(Dept dept){
deptMapper.update(dept);
deptMapper.delete(60);
int i = 122/0;
}
@Transactional(propagation = Propagation.REQUIRED,isolation = Isolation.DEFAULT,timeout=36000,rollbackFor=Exception.class)
public void delete(Integer deptno){
deptMapper.delete(deptno);
}
}
<?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.example.mybatis.mapper.DeptMapper">
<resultMap id="dept" type="com.example.mybatis.entity.Dept">
<result column="deptno" property="deptno"></result>
<result column="dname" property="dname"></result>
<result column="loc" property="loc"></result>
</resultMap>
<sql id="base-columns">
deptno,dname,loc
</sql>
<select id="queryAllDepts" resultMap="dept">
select deptno,dname,loc from dept
</select>
<select id="getAll" resultMap="dept">
select
<include refid="base-columns"></include>
from dept
</select>
<select id="getOne" resultMap="dept" parameterType="java.lang.Integer">
select
<include refid="base-columns"></include>
from dept
where deptno=#{deptno}
</select>
<insert id="insert" parameterType="com.example.mybatis.entity.Dept">
insert into dept (deptno,dname,loc) values (#{deptno},#{dname},#{loc})
</insert>
<update id="update" parameterType="com.example.mybatis.entity.Dept">
update dept set
<if test="dname!=null and dname!=''">dname=#{dname},</if>
<if test="loc!=null and loc!=''">loc=#{loc},</if>
deptno=#{deptno}
where deptno = #{deptno}
</update>
<delete id="delete" parameterType="java.lang.Integer">
delete from dept where deptno = #{deptno}
</delete>
</mapper>
spring:
datasource:
url: jdbc:oracle:thin:@localhost:1521:orcl
username: scott
password: bruce123
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 下面爲連接池的補充設置,應用到上面所有數據源中
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
max-active: 20
# 配置獲取連接等待超時的時間
max-wait: 60000
# 配置間隔多久才進行一次檢測,檢測需要關閉的空閒連接,單位是毫秒
time-between-eviction-runs-millis: 60000
# 配置一個連接在池中最小生存的時間,單位是毫秒
min-evictable-idle-time-millis: 300000
validation-query: SELECT 1 FROM DUAL
test-while-idle: true
test-on-borrow: false
test-on-return: false
# 打開PSCache,並且指定每個連接上PSCache的大小
pool-prepared-statements: true
# 配置監控統計攔截的filters,去掉後監控界面sql無法統計,'wall'用於防火牆
max-pool-prepared-statement-per-connection-size: 20
filters: stat,wall
use-global-data-source-stat: true
# 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
connect-properties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 配置監控服務器
stat-view-servlet:
# 1.1.10以後的版本需要指定爲true 不然默認是關閉的就會出現404
enabled: true
login-username: admin
login-password: 123456
reset-enable: false
url-pattern: /druid/*
# 添加IP白名單
#allow:
# 添加IP黑名單,當白名單和黑名單重複時,黑名單優先級更高
#deny:
web-stat-filter:
# 添加過濾規則
url-pattern: /*
# 忽略過濾格式
exclusions: "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*"
mybatis:
#mybatis主要配置類
config-location: classpath:mybatis-config.xml
#mapper配置xml
mapper-locations: classpath*:mapper/*.xml
#配置實體類別名
type-aliases-package: com.example.mybatis.entity
<?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>
<!-- 使用jdbc的getGeneratedKeys獲取數據庫自增主鍵值 -->
<setting name="useGeneratedKeys" value="true" />
<!-- 使用列別名替換列名 默認:true -->
<setting name="useColumnLabel" value="true" />
<!-- 開啓駝峯命名轉換:Table{create_time} -> Entity{createTime} -->
<setting name="mapUnderscoreToCamelCase" value="true" />
<!-- 打印查詢語句 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases>
</configuration>
package com.example.mybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
@MapperScan("com.example.mybatis.mapper")
@EnableTransactionManagement
public class MybatisApplication {
public static void main(String[] args) {
SpringApplication.run(MybatisApplication.class, args);
}
}