码迷,mamicode.com
首页 > 编程语言 > 详细

Spring Boot 集成Mybatis

时间:2016-07-13 17:45:20      阅读:320      评论:0      收藏:0      [点我收藏+]

标签:

Spring Boot 本身自带已经集成JPA,实现JPA规范最好的是hibernation, 最近几年中持久层框架中应用更多是MyBatis,原因呢?Hibernate 的灵活性没有Mybatis好,而且Hibernate的学习成本相对于MyBatis要高很多。

技术分享

应用按照分层的理念进行了拆分,它具有高度的可扩展性.将公共抽离出来,做成服务供其他模块调用。实现RPC 可以采用RMI、Hession,但是

但是由于服务越来越多,依赖难以管理,而且RPC调用没有负载均衡,服务心跳检查。。。救星到来。。。阿里Dubbo(RPC服务治理框架)

客户端层:客户端层包含了针对目标平台的用户界面,可能会包括基于Web的、移动端用户界面。一般来讲,这可能会是Web应用,包含诸如用户管理、会话管理、页面构建等功能,客户端所发起Http请求,都需要以RESTful服务的形式返回。

服务端:服务端包含了数据的选取,数据的来源诸如关系型数据库、Redis、memcache,对外提供服务。

应用技术:
Spring Boot

Dubbo

MyBatis

FlyWay

Zookeeper

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.dcms.cif</groupId>
	<artifactId>dcms-cif-business</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>dcms-cif-business</name>
	<url>http://maven.apache.org</url>


	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.11</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-aop</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-redis</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.13</version>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.38</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.2.8</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>dubbo</artifactId>
			<version>2.5.3</version>
		</dependency>
		<dependency>
			<groupId>org.dcms.cif</groupId>
			<artifactId>dcms-cif-service</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.dcms.cif</groupId>
			<artifactId>dcms-common</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>org.dcms.cif</groupId>
			<artifactId>dcms-cif-base</artifactId>
			<version>0.0.1-SNAPSHOT</version>
		</dependency>
		<dependency>
			<groupId>com.101tec</groupId>
			<artifactId>zkclient</artifactId>
			<version>0.4</version>
		</dependency>
		<dependency>
			<groupId>com.googlecode.flyway</groupId>
			<artifactId>flyway-core</artifactId>
			<version>2.3.1</version>
		</dependency>
	</dependencies>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>
</project>
<strong><span style="font-size:18px;">application.yml</span></strong>
server:
      port: 18080
logging:
      config: classpath:logback.xml
      path: D:\workspace\dcms-web-restful
      file: log.log
spring:
    datasource:
        name: maint
        type: com.alibaba.druid.pool.DruidDataSource
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&failOverReadOnly=false
        username: root
        password: root
        filters: stat,wall,slf4j
        maxActive: 15
        initialSize: 1
        maxWait: 10000
        useUnfairLock: true
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 30000
        validationQuery: select 'x'
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: false
        
    jackson:
        date-format: yyyy-MM-dd HH:mm:ss
        time-zone: GMT+8  
application-mybatis.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
    
    
    <aop:aspectj-autoproxy proxy-target-class="true" expose-proxy="true"/>
    
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
    
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
      <property name="basePackage" value="com.dcms.cif.mapper"/>
    </bean>
    
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
      <constructor-arg ref="sqlSessionFactoryBean"/>
    </bean>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
       <tx:attributes>
          <tx:method name="find*" read-only="true" />
          <tx:method name="select*" read-only="true"/>
          <tx:method name="get*" read-only="true"/>
          <tx:method name="submit*" propagation="REQUIRED"/>
          <tx:method name="clear*" propagation="REQUIRED"/>
          <tx:method name="create*" propagation="REQUIRED"/>
          <tx:method name="activate*" propagation="REQUIRED"/>
          <tx:method name="save*" propagation="REQUIRED"/>
          <tx:method name="insert*" propagation="REQUIRED"/>
          <tx:method name="add*" propagation="REQUIRED"/>
          <tx:method name="update*" propagation="REQUIRED"/>
          <tx:method name="delete*" propagation="REQUIRED"/>
          <tx:method name="remove*" propagation="REQUIRED"/>
          <tx:method name="execute*" propagation="REQUIRED"/>
          <tx:method name="del*" propagation="REQUIRED"/>
       </tx:attributes>
    </tx:advice>
    
    <aop:config expose-proxy="true" proxy-target-class="true" >
       <aop:pointcut expression="execution(public * com.dcms..service.*.*(..))" id="pt"/>
       <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
    </aop:config>
</beans>
dubbo-cif-providers.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.0.xsd  
    http://code.alibabatech.com/schema/dubbo
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

	<bean id="userServiceImp" class="com.dcms.cif.service.imp.UserServiceImp" />

	<!-- 应用配置,配置当前应用信息-->
	<dubbo:application name="cif-providers"  />

        <!-- 配置中心,配置注册中心 -->
	<dubbo:registry  protocol="zookeeper" address="127.0.0.1:2181" ></dubbo:registry>

        <!--  协议配置,用于配置提供者的协议,协议由提供方指定,消费放被动接受 -->
	<dubbo:protocol name="dubbo" port="28888"></dubbo:protocol>
	
	<!-- 暴露一个服务,定义服务的原信息,一个服务可以注册到多个注册中心。一个服务可以用多个协议暴露 -->
	<dubbo:service interface="com.dcms.cif.service.UserService" ref="userServiceImp" timeout="3000">
	    <dubbo:method name="findUserById" timeout="2000"></dubbo:method>
	</dubbo:service>
</beans>
UserMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"
"http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
<mapper namespace="com.dcms.cif.mapper.UserMapper">

	<resultMap type="com.dcms.cif.domain.CifVO" id="RM_CIF">
		<id column="ID" property="id" />
		<result column="NM" property="nm" />
		<result column="AGE" property="age" />
		<result column="TELEPHONE" property="telephone" />
		<result column="EMAIL" property="email" />
		<result column="CB_HOST_NO" property="cbHostNo" />
		<result column="DT_CREATED" property="dtCreated" />
		<result column="CREATED_BY" property="createdBy" />
		<result column="DT_UPDATED" property="dtUpdated" />
		<result column="UPDATE_BY" property="updatedBy" />
	</resultMap>

	<sql id="allcolumn">
        <![CDATA[
        ID,
        NM,
        AGE,
        TELEPHONE,
        EMAIL,
        CB_HOST_NO,
        DT_CREATED,
        CREATED_BY,
        DT_UPDATED,
        UPDATE_BY      
        ]]>
	</sql>

	<insert id="insert" useGeneratedKeys="true" keyProperty="id">
	    <![CDATA[
	        INSERT INTO CIF
	        (
	         ID,
             NM,
             AGE,
             TELEPHONE,
             EMAIL,
             CB_HOST_NO,
             DT_CREATED,
             CREATED_BY,
             DT_UPDATED,
             UPDATE_BY 
	        )VALUES(
	          #{id},
	          #{nm},
	          #{age},
	          #{telephone},
	          #{email},
	          #{cbHostNo},
	          #{dtCreated},
	          #{createdBy},
	          #{dtUpdated},
	          #{updatedBy}
	        )
	     ]]>
	</insert>
	<select id="findById" resultMap="RM_CIF">
		select
		<include refid="allcolumn" />
		from cif where cif.id = #{id}
	</select>
	<select id="findUserByNm" resultMap="RM_CIF">
		select
		<include refid="allcolumn" />
		from cif where cif.nm like '${nm}%'
	</select>

	<select id="findUserByOwner" resultMap="RM_CIF">
		select
		<include refid="allcolumn" />
		from cif where cif.nm in
		<foreach collection="owners" item="owner" index="index" open=" (" close=")" separator=",">
			#{owner}
		</foreach>
	</select>

</mapper>
package com.dcms.cif.mapper;

import java.util.List;

import com.common.mapper.BaseMapper;
import com.dcms.cif.domain.CifVO;

public interface UserMapper extends BaseMapper<CifVO>
{
    /**
     * @param nm
     * @return List<CifVO>
     * @description: 根据指定nm去模糊查询记录,结果可能是多条数据
     */
    public List<CifVO> findUserByNm(String nm);
    
    /**
     * @param owner
     * @return List<CifVO>
     * @description: 根据指定nm去模糊查询记录,结果可能是多条数据
     * 
     */
    public List<CifVO> findUserByOwner(String... owners);
}
package com.dcms.cif.service.imp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.dcms.cif.domain.CifVO;
import com.dcms.cif.mapper.UserMapper;
import com.dcms.cif.service.UserService;

public class UserServiceImp implements UserService
{
    public static final Logger logger = LoggerFactory.getLogger(UserServiceImp.class);
    
    @Autowired
    private UserMapper userMapper;
       
    public CifVO findUserById(int id)
    {
        return userMapper.findById(id);
    }   
}

package com.dcms.cif;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@ImportResource(value = {"classpath:dubbo-cif-providers.xml", "classpath:application-mybatis.xml"})
public class Application
{
    public static void main(String[] args)
    {
        SpringApplication.run(Application.class, args);
    }
}






   





Spring Boot 集成Mybatis

标签:

原文地址:http://blog.csdn.net/manmanxiaohui/article/details/51883199

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!