mybatis与spring集成ssm01

mybatis与spring集成

其实就是将SqlSessionFactory的创建交给spring管理

1. 添加相关依赖

<?xml version="1.0" encoding="UTF-8"?>

<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>com.zking</groupId>
    <artifactId>ssm1</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>ssm1 Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <spring.version>5.0.2.RELEASE</spring.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- ********************** spring依赖 ********************** -->
        <!--1)spring核心依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--spring ioc依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--spring 扩展依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--2)spring dao层依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>${spring.version}
            </version>
        </dependency>
        
        <!--3)aop相关依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--4)spring web相关依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--5) spring test相关依赖 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>${spring.version}</version>
        </dependency>
        
        <!--6)springMVC框架-->
        <dependency>
	        <groupId>org.springframework</groupId>
	        <artifactId>spring- webmvc</artifactId>
	        <version>${spring.version}</version>
        </dependency>


        <!-- ********************** junit 单元测试依赖 ********************** -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- ********************** Java Servlet API  ********************** -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.0</version>
            <scope>provided</scope>
        </dependency>

        <!-- ********************** Mybatis依赖 ********************** -->
        <!--1) mybatis核心 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        
        <!--2) Mybatis分页插件-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.2</version>
        </dependency>
        
        <!--3) spring整合mybatis -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>

        <!-- ********************** Mysql JDBC驱动 ********************** -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.44</version>
        </dependency>

        <!--dbcp2连接池依赖-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
            <version>2.4.3</version>
        </dependency>

        <!-- **********************  日志 配置  ********************** -->
        <!--记得修改mybatis.cfg.xml添加如下内容-->
        <!--<setting name="logImpl" value="LOG4J2"/>-->
        
        <!--核心log4j2jar包-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.9.1</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.9.1</version>
        </dependency>
        
        <!--web工程需要包含log4j-web,非 web工程不需要-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>2.9.1</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>ssm1</finalName>
        <resources>
            <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <!--解决mybatis-generator-maven-plugin运行时没有将jdbc.properites文件放入target文件夹的问题-->
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>jdbc.properties</include>
                    <include>*.xml</include>
                </includes>
            </resource>
        </resources>

        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generator</groupId>
                    <artifactId>mybatis-
                        generator-maven-plugin</artifactId>
                    <version>1.3.2</version>
                    <dependencies>
                        <!--使用Mybatis-generator插件不能使用太高版本的mysql驱动-->
                        <dependency>
                            <groupId>mysql</groupId>
                            <artifactId>mysql-connector-java</artifactId>
                            <version>5.1.44</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <overwrite>true</overwrite>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
1.2 spring整合mybatis
     <dependency>
           <groupId>org.mybatis</groupId>
           <artifactId>mybatis-spring</artifactId>
           <version>1.3.1</version>
     </dependency>

1.3 添加dbcp2连接池依赖
    <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-dbcp2</artifactId>
           <version>2.1.1</version>
     </dependency>
     <dependency>
           <groupId>org.apache.commons</groupId>
           <artifactId>commons-pool2</artifactId>
           <version>2.4.3</version>
     </dependency>

2. 创建spring配置文件spring.xml,并进行相关配置

  1. 注解式开发
  2. 引入外置jdbc配置文件
  3. dbcp2数据库连接池配置
  4. spring和MyBatis整合
  5. 注解式事务配置开始 <tx:annotation-driven transaction-manager=“transactionManager”/>

3. 注解式开发
@Repository 仓库一般存在于dao层
@Service 服务的意思
@Constroller 控制层

@Component 万能注解,如果你再类上面加了这个注解,它表示这一个主键它交给spring管理
@Scope 范围的意思,就是表示范围多列模式和单列模式

@Autowired
@Resource
注1:各个注解的作用请参考资料“注解式开发”

@Transactional
注2:个人感觉注解式事务比以前的声明式事务更加麻烦,要写的东西更多

4. Spring Test+JUnit完美组合
4.1 在工程的pom文件中增加spring-test的依赖

<dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring.version}</version>
</dependency>

4.2 创建BaseTestCase,并在该类上加上两个注解:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={“classpath:spring.xml”})

4.3 使用idea的junit插件(JUnitGenerator V2.0)生成测试用例,
注1:还需要修改相关配置,详情参考资料:“IDEA Junit4配置.mht”
注2:快捷键,Ctrl + Shift + T

4.4 在执行单元测试时,为了避免产生脏数据,可将测试单元设置成事务回滚
@Rollback(value = true|false)//true回滚事务,false提交事务
@Transactional(transactionManager = “transactionManager”)

5. 修改分页代码:
使用AOP编程解决分页代码重复的问题
注1:AOP注解式开发详情见资料“Spring如何实现AOP环绕通知.mht”
注2:AOP分页实现“PageBeanAsp“

MAVEN指令:mybatis-generator:generate -e

实体类类的ToString&&set&&get的注解方法插件

<dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.0</version>
      <scope>provided</scope>
</dependency>
©️2020 CSDN 皮肤主题: 1024 设计师:上身试试 返回首页