微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Spring和mybatis的整合

一、搭建项目开发环境

1. 新建一个maven项目SpringMybatis,项目结构如下:

                                                                               

说明:

src/main/java 存放java代码和映射文件

         com.study.springmybatis.dao 存放mapper接口

         com.study.springmybatis.mapper 存放mapper映射文件

         com.study.springmybatis.model 存放pojo类

         com.study.springmybatis.service 存放service接口和对应的实现类

src/test/java存放测试代码

src/main/resources 存放数据库配置文件和xml配置文件

2. 在pom.xml文件引入spring和mybatis相关的依赖,这里用的是阿里的maven远程仓库http://maven.aliyun.com/nexus/content/groups/public/

4.0.0 com.study.springmybatis SpringMybatis 0.0.1-SNAPSHOT jar SpringMybatis http://maven.apache.org .sourceEncodingUTF-8.sourceEncoding 添加Spring相关的依赖 begin org.springframework spring-core 4.3.12.RELEASE org.springframework spring-context 4.3.12.RELEASE org.springframework spring-tx 4.3.12.RELEASE org.springframework spring-jdbc 4.3.12.RELEASE org.springframework spring-test 4.3.12.RELEASE org.springframework spring-aop 4.3.12.RELEASE org.springframework spring-beans 4.3.12.RELEASE org.apache.geronimo.bundles aspectjweaver 1.6.8_2 添加Spring相关的依赖 end 添加mybatis的核心包 begin org.mybatis mybatis 3.2.8 添加mybatis的核心包 end 添加mybatis与Spring整合的核心包 begin org.mybatis mybatis-spring 1.2.0 添加mybatis与Spring整合的核心包 end 数据库连接相关包 begin org.wisdom-framework mysql-connector-java 5.1.34_1 commons-dbcp commons-dbcp 1.4 commons-pool commons-pool 1.6 c3p0 c3p0 0.9.1.2 数据库连接相关包 end commons-logging commons-logging 1.1.3 junit junit 4.12 copetestcope

sql和使用mybatis逆向工程生成代码

1.建表

`id` () `username` () COMMENT 用户名称 `birthday` date COMMENT `sex` () COMMENT `address` () COMMENT ) ENGINEMyISAM AUTO_INCREMENT CHARSETutf8 COMMENT用户信息表;

2. 使用mybatis逆向工程生成代码

   mybatis逆向工程能根据数据库的表结构生成对应的mapper映射文件,接口,实体类,具体操作方法可以看我的前一篇文章,然后修改生成文件名称为符合java命名规范的名称

3. 生成逆向工程后的代码目录结构如下,这里使用的是spring的注解管理bean

   生成的mapper接口UserDao.java代码

usermodel; 用户接口 @Repository("userDao" usermodel record); usermodel record); usermodel selectByPrimaryKey(Integer id); usermodel record); usermodel record); }

生成的mapper映射文件mapper-user.xml,因为之前对生成的实体和接口做了名称修改,所以这里也要对mapper-user.xml里面的内容做对应的修改修改后的内容如下

usermodel" sql sql arameterTypejava.lang.Integer" arameterTypejava.lang.Integer" arameterTypeusermodel" arameterTypeusermodel" arameterTypeusermodel" arameterTypeusermodel"

生成的pojo即实体类usermodel.java

用户信息表 usermodel { 用户名称 .id = .username = username == ? .birthday = .sex = sex == ? .address = address == ? }

业务的接口UserServiceI.java和实现类UserService.java

usermodel; 用户id获取用户 aram usermodel getUserById(Integer userId); }
usermodel; @Service("userService" UserService 自动注入UserDao usermodel getUserById(Integer userId) { }

三、在src/main/resources目录下编写配置文件

1. 数据库配置dbconfig.properties

MysqL.jdbc.Driver MysqL://192.168.152.1:3306/study?characterEncoding=utf-8 validationQuery=SELECT 1

2. Spring框架的核心配置文件spring.xml

spring-beans-3.0.xsd 配置文件 fig.properties属性文件 property-placeholder sspath:dbconfig.properties" 自动扫描(自动注入),扫描com.study.springmybatis这个包以及它的子包的所有使用spring注解标注的类

3. Spring框架与Mybatis框架整合的配置文件

spring-beans-3.0.xsd 配置文件 配置数据源 ${driver} ${jdbc_url} ${jdbc_username} ${jdbc_password} 分隔线========================================= sqlSessionFactory sqlSessionFactory"sqlSessionfactorybean" sqlSessionFactory时需要使用上述配置好的数据源以及sql映射文件 自动扫描com/study/springmybatis/mapper/目录下的所有sql映射的xml文件,省掉sqlMapConfig.xml里的手工配置 sspath(类路径)下com.study.springmybatis.mapper包中的所有xml文件 sql映射的xml文件位于com.study.springmybatis.mapper包下,这样就可以被自动扫描 sspath:com/study/springmybatis/mapper/*.xml" figurer" sqlSessionfactorybeanName"sqlSessionFactory" 分隔线========================================= 拦截器方式配置事物 required" required" required" required" required" required" required" required" required" required" required" fig pointcut pointcut" pointcut-refpointcut" fig

四、在src/test/java编写测试类SpringMyBatisTest,本来测试类是放在src/test/java目录下面的。但是不知道为啥找不到junit的注解@Test,这里就偷了一下懒移到src/main/java目录的com.study.springmybatis.test包下面了

figuration; usermodel; @RunWith(SpringJUnit4ClassRunner. figuration注解并使用该注解的locations属性指明spring和配置文件之后, @ContextConfiguration(locations = {"classpath:spring.xml","classpath:spring-mybatis.xml" userId = 1 usermodel user = System.out.println("查询到的用户名为:"+ }

运行测试类的结果为:

十月 22,2017 3:08:00 信息: Loaded TestExecutionListener names from location [meta-inf/sqlScriptsTestExecutionListener] 十月 22,2017 3:08:00 信息: Could not instantiate TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener]. Specify custom listener classes or make the listener classes (and their required dependencies) available. Offending : [javax/servlet/ 十月 22,2017 3:08:00 sqlScriptsTestExecutionListener@34ce8af7] 十月 22,2017 3:08:00FinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from 十月 22,2017 3:08:00FinitionReader loadBeanDeFinitions 信息: Loading XML bean deFinitions from path resource [spring- 十月 22,2017 3:08:00 信息: Refreshing org.springframework.context.support.GenericApplicationContext@5a61f5df: startup date [Sun Oct 22 15:08:00 CST 2017 查询到的用户名为:lgs

五、需要完整工程代码的同学去我的githup上下载https://github.com/leeSmall/SpringMybatis

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐