SpringBoot与Mybatis整合的多模块项目

springBoot项目构建

Spring多模块项目的构建

1.使用Idea构建一个Springboot项目

File-->new-->project-->springInitializr-->(NEXT)java

2.主项目pom中添加依赖

<!-- Spring Boot Web 依赖 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>

	<!-- Spring Boot Test 依赖 -->
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-test</artifactId>
		<scope>test</scope>
	</dependency>

3.建立各个功能模块

主项目右键-->new Module-->Mavenmysql

4.web模块

主项目右键-->new Module-->springInitializr-->SpringBoot Web项目git

修改parent为主项目parent,能够去掉重复的依赖github

删除主项目的src等文件夹,能够将主项目中的build模块移到web项目中web

5.运行

设置好各模块中的依赖关系,能够将不一样业务的代码放在不一样的模块中,Springboot的默认加载类会扫描其目录下全部Spring相关注解,因此本身写的各种须要在主Application所在的包下。spring

web模块在打包时,默认为jar打包。若是想使用自带的Tomcat做为运行服务器,那么能够打包成war包,使用Tomcat访问。sql

2、SpringBoot与Mybatis的整合

  1. 在pom文件中添加对mybatis、mysql链接池等jar包的依赖
<!-- Spring Boot Mybatis 依赖 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis-spring-boot}</version>
		</dependency>

		<!-- Druid 数据链接池依赖 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>${druid}</version>
		</dependency>

		<!-- MySQL 链接驱动依赖 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql-connector}</version>
		</dependency>
  1. 对web模块下的Springboot配置文件进行配置,主要是数据库的信息数据库

  2. 配置DruidDataSource的配置文件,采用@Configuration类的形式api

  3. 写各个层的方法spring-mvc

    大坑

    mysql数据库对应的版本问题,使用8.x版本数据库根本不能完成Druid的数据源链接

    注意pom文件中导入的依赖包,其实并很少,不要搞昏了头

    Dao层代码注解@Mapper

    项目Github地址

3、SpringBoot的配置文件

Spring Boot 提供了对应用进行自动化配置。相比之前 XML 配置方式,不少显式方式申明是不须要的。两者,大多数默认的配置足够实现开发功能,从而更快速开发。

Spring Boot 不仅仅从 application.properties 获取配置,因此咱们能够在程序中多种设置配置属性。按照如下列表的优先级排列:

1.命令行参数

2.java:comp/env 里的 JNDI 属性

3.JVM 系统属性

4.操做系统环境变量

5.RandomValuePropertySource 属性类生成的 random.* 属性

6.应用之外的 application.properties(或 yml)文件

根据这个在多 moudle 的项目中,好比常见的项目分 api 、service、dao 等 moudles,每每会加一个 deploy moudle 去打包该业务各个子 moudle,应用之外的配置优先。

7.打包在应用内的 application.properties(或 yml)文件

8.在应用 @Configuration 配置类中,用 @PropertySource 注解声明的属性文件

9.SpringApplication.setDefaultProperties 声明的默认属性


springboot能够在不一样的环境,设置有多个环境的配置:

application-dev.properties:开发环境
application-prod.properties:生产环境

Spring Boot 是经过 application.properties 文件中,设置 spring.profiles.active 属性,好比 ,配置了 dev ,则加载的是 application-dev.properties :

# Spring Profiles Active
spring.profiles.active=dev

@Configuration

springboot推荐使用用java代码的形式申明注册bean。 @Configuration注解能够用java代码的形式实现spring中xml配置文件配置的效果。

@Configuration
public class TestMybaitsConf {
    @Bean
    public DataSource dataSource() {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://192.168.100.25:6660/TXSMS?useUnicode=true&amp;characterEncoding=utf-8");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return dataSource;
    }
    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) {
        SqlSessionFactory factory = null;
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setConfigLocation(resolver.getResource("classpath:mybatis.xml"));
        try {
            factory = bean.getObject();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return factory;
    }
    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

使用xml注册bean

@Configuration
@ImportResource("classpath:spring-mybatis.xml")
public class TestMybaitsConf {

}

使用的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:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc  http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/jee  http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
        http://www.springframework.org/schema/beans  http://www.springframework.org/schema/beans/spring-beans-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://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://192.168.100.25:6660/TXSMS?useUnicode=true&amp;characterEncoding=utf-8"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="configLocation" value="classpath:mybatis.xml"></property>
    </bean>

    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
相关文章
相关标签/搜索