首页 > 编程语言 > 详细

mybatis-spring整合

时间:2018-03-19 23:58:11      阅读:666      评论:0      收藏:0      [点我收藏+]

技术分享图片1. 配置jar包

Spring,mybatis,mybatis-spring,mysql等。。。

<properties>

              <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

              <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

 

              <!-- spring版本号 -->

              <spring.version>4.2.5.RELEASE</spring.version>

 

              <!-- mybatis版本号 -->

              <mybatis.version>3.2.8</mybatis.version>

 

              <!-- mysql驱动版本号 -->

              <mysql-driver.version>5.1.29</mysql-driver.version>

 

              <!-- log4j日志包版本号 -->

              <slf4j.version>1.7.18</slf4j.version>

              <log4j.version>1.2.17</log4j.version>

 

       </properties>

 

 

       <dependencies>

              <!-- 数据库连接池包 -->

              <dependency>

                     <groupId>com.mchange</groupId>

                     <artifactId>c3p0</artifactId>

                     <version>0.9.5-pre8</version>

              </dependency>

              <!-- 添加jstl依赖 -->

              <dependency>

                     <groupId>jstl</groupId>

                     <artifactId>jstl</artifactId>

                     <version>1.2</version>

              </dependency>

 

              <dependency>

                     <groupId>javax</groupId>

                     <artifactId>javaee-api</artifactId>

                     <version>7.0</version>

              </dependency>

 

              <!-- 添加junit4依赖 -->

              <dependency>

                     <groupId>junit</groupId>

                     <artifactId>junit</artifactId>

                     <version>4.11</version>

                     <!-- 指定范围,在测试时才会加载 -->

                     <scope>test</scope>

              </dependency>

 

              <!-- 添加spring核心依赖 -->

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-core</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-web</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-oxm</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-tx</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-jdbc</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-webmvc</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-context</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-context-support</artifactId>

                     <version>${spring.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-aop</artifactId>

                     <version>${spring.version}</version>

              </dependency>

 

              <dependency>

                     <groupId>org.springframework</groupId>

                     <artifactId>spring-test</artifactId>

                     <version>${spring.version}</version>

              </dependency>

 

              <!-- 添加mybatis依赖 -->

              <dependency>

                     <groupId>org.mybatis</groupId>

                     <artifactId>mybatis</artifactId>

                     <version>${mybatis.version}</version>

              </dependency>

 

              <!-- 添加mybatis/spring整合包依赖 -->

              <dependency>

                     <groupId>org.mybatis</groupId>

                     <artifactId>mybatis-spring</artifactId>

                     <version>1.2.2</version>

              </dependency>

 

              <!-- 添加mysql驱动依赖 -->

              <dependency>

                     <groupId>mysql</groupId>

                     <artifactId>mysql-connector-java</artifactId>

                     <version>${mysql-driver.version}</version>

              </dependency>

              <!-- 添加数据库连接池依赖 -->

              <dependency>

                     <groupId>commons-dbcp</groupId>

                     <artifactId>commons-dbcp</artifactId>

                     <version>1.2.2</version>

              </dependency>

 

              <!-- 添加fastjson -->

              <dependency>

                     <groupId>com.alibaba</groupId>

                     <artifactId>fastjson</artifactId>

                     <version>1.1.41</version>

              </dependency>

 

              <!-- 添加日志相关jar包 -->

              <dependency>

                     <groupId>log4j</groupId>

                     <artifactId>log4j</artifactId>

                     <version>${log4j.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.slf4j</groupId>

                     <artifactId>slf4j-api</artifactId>

                     <version>${slf4j.version}</version>

              </dependency>

              <dependency>

                     <groupId>org.slf4j</groupId>

                     <artifactId>slf4j-log4j12</artifactId>

                     <version>${slf4j.version}</version>

              </dependency>

 

              <!-- log end -->

              <!-- 映入JSON -->

              <dependency>

                     <groupId>org.codehaus.jackson</groupId>

                     <artifactId>jackson-mapper-asl</artifactId>

                     <version>1.9.13</version>

              </dependency>

            <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->

              <dependency>

                     <groupId>com.fasterxml.jackson.core</groupId>

                     <artifactId>jackson-core</artifactId>

                     <version>2.8.0</version>

              </dependency>

              <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->

              <dependency>

                     <groupId>com.fasterxml.jackson.core</groupId>

                     <artifactId>jackson-databind</artifactId>

                     <version>2.8.0</version>

              </dependency>

 

              <dependency>

                     <groupId>commons-fileupload</groupId>

                     <artifactId>commons-fileupload</artifactId>

                     <version>1.3.1</version>

              </dependency>

 

              <dependency>

                     <groupId>commons-io</groupId>

                     <artifactId>commons-io</artifactId>

                     <version>2.4</version>

              </dependency>

 

              <dependency>

                     <groupId>commons-codec</groupId>

                     <artifactId>commons-codec</artifactId>

                     <version>1.9</version>

              </dependency>

 

              <!-- https://mvnrepository.com/artifact/com.github.abel533/ECharts -->

              <dependency>

                     <groupId>com.github.abel533</groupId>

                     <artifactId>ECharts</artifactId>

                     <version>3.0.0</version>

              </dependency>

 

              <dependency>

                     <groupId>com.google.code.gson</groupId>

                     <artifactId>gson</artifactId>

                     <version>2.5</version>

                     <scope>compile</scope>

                     <optional>true</optional>

              </dependency>

 

              <dependency>

                     <groupId>net.sf.jxls</groupId>

                     <artifactId>jxls-core</artifactId>

                     <version>1.0.5</version>

              </dependency>

              <dependency>

            <groupId>org.mybatis.generator</groupId>

            <artifactId>mybatis-generator-core</artifactId>

            <version>1.3.2</version>

        </dependency>

       </dependencies>

 

       <build>

              <plugins>

                     <plugin>

                            <groupId>org.mybatis.generator</groupId>

                            <artifactId>mybatis-generator-maven-plugin</artifactId>

                            <version>1.3.2</version>

                            <configuration>

                                   <verbose>true</verbose>

                                   <overwrite>true</overwrite>

                            </configuration>

                     </plugin>

              </plugins>

              <finalName>ssm4</finalName>

       </build>

2.配置文件

2.1applicationContext.xml

 技术分享图片

 

2.1.1第一是要引入数据文件jdbc.properties:保存数据库连接参数的信息

2.1.2第二配置数据源,这里配置了dbcp连接池

2.1.3配置SqlSessionFactory,让spring自动生成管理,这里要引用数据源和mybatis的配置文件

2.1.4配置Dao接口,class指向实现类全称

2.2mybatis.xml配置

 技术分享图片

 

2.2.1加载配置文件

 技术分享图片

 

2.3jdbc.porperties

 技术分享图片

 

2.4log4j.porperties

 技术分享图片

 

3.测试

3.1项目结构

 技术分享图片

 

3.1.1UserDao接口

 技术分享图片

 

3.1.2UserDaoImpl实现类(实现类要继承SqlSessionDaoSupport类)

 技术分享图片

 

3.1.3测试类

 技术分享图片

 

4.Mapper代理配置

4.1修改applicationContext.xml

 技术分享图片

 

(1)    第一种配置mapper,每个mapper都要配置一次,工程重复繁琐

(2)    第二种批量配置mapper,只要指定basepackage的包,就会到这个下面去扫描,多个包用半角逗号隔开

4.2修改mybatis.xml

 技术分享图片

 

(1)注释掉mapper文件的扫描

5.mapper代理测试

5.1UserMapper.java

 技术分享图片

 

5.2UserMapper.xml

 技术分享图片

 

5.3测试

 技术分享图片

 

5.3.1加载applicationContext.xml文件

5.3.2获取Mapper代理对象

5.3.3使用Mapper的方法操作数据库

6.使用mybatis generator自动化工具创建Mapper.java和xml文件

6.1配置generator.xml配置文件

 技术分享图片

 

6.2運行java代碼生成代碼

 技术分享图片

 

注意:file对应的位置是6.1配置文件的位置

6.3刷新工程

 技术分享图片

 

6.3.1po包下的Example类是用于条件查询

6.3.2测试

6.3.2.1查询测试

 技术分享图片

 

(1)      一般的查询是一样的

(2)      条件查询的步骤是:

  1. 创建example对象,
  2. 生成criteria对象
  3. 通过criteria对象拼凑条件
  4. 通过mapper去调用相关的查询方法传入example对象

6.3.2.2更新测试

 技术分享图片

 

(1)      updateByPrimaryKey需要先查询才能更新

(2)      updateByPrimaryKeySelective不需要先查询才能更新,一般用于批量更新

 

mybatis-spring整合

原文:https://www.cnblogs.com/jnxj/p/8605902.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!