springboot整合Druid之三:com.alibaba.druid-spring-boot-starter

<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);
    }

}

发布了152 篇原创文章 · 获赞 78 · 访问量 50万+

猜你喜欢

转载自blog.csdn.net/qiuzhi__ke/article/details/105321421
今日推荐