springboot+mybatis+druid+postgreSql案例
springboot+mybatis+druid+postgreSql/mysql ⽰例
简介
最近加⼊⼀个新的团队,发现团队开发的底层调⽤时通过jdbctemplate拼凑sql,不能说这个不好,但是对于开发效率来说,肯定没有orm 框架使⽤来的6,jdbctemplate拼凑造轮⼦的话,⼤部分时间会花在轮⼦上,⽽且,压测的时候,sql的好坏,对业务代码的嵌⼊太深,综合考虑不划算。
其实,就现在世⾯主流构建来说,都是mybatis,这个主要是把sql语句独⽴出来了,做到了对sql语句的可控。
啥也不说,直接看项⽬结构:
结构解析
spring:
profiles:
active: dev
# 服务端⼝
server:
port: 9090
servlet:
context-path: /mptest
#数据库配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://192.168.100.103:5432/postgres
username: postgres
password: postgres
druid:
#初始化连接⼤⼩
initial-size: 8
#最⼩空闲连接数
min-idle: 5
#最⼤连接数
max-active: 10
#查询超时时间
query-timeout: 6000
#事务查询超时时间
transaction-query-timeout: 6000
#关闭空闲连接超时时间
remove-abandoned-timeout: 1800
filters: stat,config
#mybatis
mybatis:
mapper-locations: classpath:com/examle/mpdemo/dao/*.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- ⽂件输出格式 -->
<property name="PATTERN"value="%-12(%d{yyyy-MM-dd HH:mm:ss.SSS}) |-%-5level [%thread] %c [%L] -| %msg%n" /> <!-- test⽂件路径 -->
<property name="TEST_FILE_PATH"value="/logs/tianjun" />
<!-- pro⽂件路径 -->
<property name="PRO_FILE_PATH"value="/logs/tianjun" />
<!-- 开发环境 -->
<springProfile name="dev">
<appender name="CONSOLE"class="ch.ConsoleAppender">
<encoder>
<pattern>${PATTERN}</pattern>
</encoder>
</appender>
<logger name="ian.canal2kafkamon.ClusterCanal"level="debug"/>
<logger name="ian.canal2kafkamon.AbstractCanalClient"level="debug"/>
<logger name="ian.canal2kafkamon.CanalKafkaProducer"level="debug"/>
<root level="info">
<appender-ref ref="CONSOLE" />
</root>
</springProfile>
<!-- 测试环境 -->
<springProfile name="test">
<!-- 每天产⽣⼀个⽂件 -->
<appender name="TEST-FILE"class="ch.olling.RollingFileAppender">
<!-- ⽂件路径 -->
<file>${TEST_FILE_PATH}</file>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<!-- ⽂件名称 -->
<fileNamePattern>${TEST_FILE_PATH}/info.%d{yyyy-MM-dd}.log</fileNamePattern>
<!-- ⽂件最⼤保存历史数量 -->
<MaxHistory>100</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${PATTERN}</pattern>
</layout>
</appender>
<root level="info">
<appender-ref ref="TEST-FILE" />
</root>
</springProfile>
<!-- ⽣产环境 -->
<springProfile name="prod">
<appender name="PROD_FILE"class="ch.olling.RollingFileAppender">
<file>${PRO_FILE_PATH}</file>
<rollingPolicy class="ch.olling.TimeBasedRollingPolicy">
<fileNamePattern>${PRO_FILE_PATH}/warn.%d{yyyy-MM-dd}.log</fileNamePattern>
<MaxHistory>100</MaxHistory>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>${PATTERN}</pattern>
</layout>
</appender>
<root level="warn">
<appender-ref ref="PROD_FILE" />
</root>
</springProfile>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:aop="/schema/aop"xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="
/schema/beans
/schema/beans/spring-beans.xsd
/schema/aop
/schema/aop/spring-aop.xsd">
<!-- 配置_Druid和Spring关联监控配置 -->
<bean id="druid-stat-interceptor"
class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>
<!-- ⽅法名正则匹配拦截配置 -->
<bean id="druid-stat-pointcut"class="org.springframework.aop.support.JdkRegexpMethodPointcut"
scope="prototype">
<property name="patterns">
<list>
springboot aop
<value&education.mapper.*</value>
</list>
</property>
</bean>
<aop:config proxy-target-class="true">
<aop:advisor advice-ref="druid-stat-interceptor"
pointcut-ref="druid-stat-pointcut" />
</aop:config>
</beans>
m-p-demo\src\main\resources\com\example\mpdemo\l:这是Dao的相关mapper类
的映射集合,idea中有个问题,在resources资源⽂件中,必须更Dao类路径取名⼀致,以便和Dao类编译在⼀起(不然会报mapper不到绑定类的坑)。
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-////DTD Mapper 3.0//EN" "/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="ample.mpdemo.dao.UserDao" >
<insert id="userInsert"parameterType="ity.User">
insert into tianjun_test (id,name,age) values (#{user.id},#{user.name},#{user.age})
</insert>
<select id="queryById"resultType="ity.User">
select * from tianjun_test where id = #{id}
</select>
</mapper>
代码剖析
还是按照entity-dao-servicej-controller的结构
entity层
ity;
/**
* @Description: ⽤户实体
* @Author: tianjun
* @CreateTime: 2018-04-02 13:22
* @Editor: tianjun
* @UpdateTime: 2018-04-02 13:22
* @Company: netposa
*/
public class User {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
Dao层
ample.mpdemo.dao;
ity.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
/**
* @Description: ⽤户相关操作
* @Author: tianjun
* @CreateTime: 2018-04-02 13:25
* @Editor: tianjun
* @UpdateTime: 2018-04-02 13:25
* @Company: netposa
*/
@Mapper
public interface UserDao {
Integer userInsert(@Param("user") User user);
User queryById(@Param("id") Integer id);
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。