Mybatis-Spring
来源:互联网 发布:asp在线客服系统源码 编辑:程序博客网 时间:2024/04/29 20:28
Spring整合Mybatis:顺应下时代潮流,把这些市面上流行的东东都整一遍玩玩。要不然就变成“老古董”了。
第一步、需要导入spring jar包、Mybatis jar包、Mybatis-spring jar包;
第二步、在spring的配置文件applicationContext.xml中加入如下配置项,及其作用:
<!-- 配置数据源 -->
<span style="white-space:pre"></span><bean id="dataSource" destroy-method="close" class="org.apache.tomcat.dbcp.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="initialSize" value="3"/></bean>
<span style="white-space:pre"></span><!-- 在spring的context中注入<span style="font-family: Arial, Helvetica, sans-serif;">SqlSessionFactory,以便在后面的sql执行模板中Injection</span><span style="font-family: Arial, Helvetica, sans-serif;"> --></span><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="dataSource"><ref bean="dataSource"/></property><property name="configLocation" value="classpath:Mybatis-Configuration.xml"></property></bean>
<span style="white-space:pre"></span><!-- 提供一个template来执行业务接口方法和配置的sql --><bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true"><property name="sqlSessionFactory"><ref bean="sqlSessionFactory"/></property></bean><bean id="userDao" parent="baseMapper"><property name="mapperInterface" value="com.cmpsky.mybatistest.dao.UserDao"></property></bean><bean id="bigUserDao" parent="baseMapper"><property name="mapperInterface" value="com.cmpsky.mybatistest.dao.BigUserDao"></property></bean>
第三步、Mybatis的基本configure文件还是要的,只是不再在里面配置包括datasource在内的environment了:
<pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//En""http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <mappers> <!-- import UserMapper.xml --><mapper resource="com/cmpsky/mybatistest/data/UserMapper.xml"/><mapper resource="com/cmpsky/mybatistest/data/BigUserMapper.xml"/> </mappers></configuration>
第四步、配置具体的SQL映射文件<span style="font-family: Arial, Helvetica, sans-serif;">BigUserMapper.xml,这里是个稍微复杂点的例子</span><span style="font-family: Arial, Helvetica, sans-serif;">:</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.cmpsky.mybatistest.dao.BigUserDao"><resultMap type="com.cmpsky.mybatistest.data.GFModel" id="gfModelMapper"><id property="gf_name" column="gf_name"/><result property="sex" column="sex"/><result property="age" column="age"/><result property="bf_name" column="bf_name"/></resultMap><resultMap type="com.cmpsky.mybatistest.data.FriendsModel" id="friendsModelMapper"><id property="f_name" column="f_name"/><result property="sex" column="sex"/><result property="age" column="age"/><result property="fname_to" column="fname_to"/></resultMap><resultMap type="com.cmpsky.mybatistest.data.BigUserModel" id="bigUserModelMapper"><constructor><idArg column="username" javaType="String"/></constructor><result column="sex" javaType="int"/><result column="age" javaType="int"/><association property="gf" column="gf_name" javaType="com.cmpsky.mybatistest.data.GFModel" resultMap="gfModelMapper"/><collection property="friendsList" column="f_name" ofType="com.cmpsky.mybatistest.data.FriendsModel" resultMap="friendsModelMapper"></collection></resultMap><select id="getBigUser" parameterType="com.cmpsky.mybatistest.data.UserModel" resultMap="bigUserModelMapper"> select tut.username,tut.sex,tut.age, tgf.gf_name,tgf.sex,tgf.age,tgf.bf_name, tfs.f_name,tfs.sex,tfs.age,tfs.fname_to from t_user_test tut left join t_gf tgf on tut.username = tgf.bf_name left join t_friends tfs on tut.username = tfs.fname_to where tut.username = #{username}</select></mapper>
第五步、定义dao接口:
<pre name="code" class="java">import com.cmpsky.mybatistest.data.BigUserModel;import com.cmpsky.mybatistest.data.UserModel;public interface BigUserDao {BigUserModel getBigUser(UserModel user);}
第六步、可以调用测试了:
import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import com.cmpsky.mybatistest.dao.BigUserDao;import com.cmpsky.mybatistest.dao.UserDao;import com.cmpsky.mybatistest.data.UserModel;import com.cmpsky.mybatistest.util.ApplicationContextFactory;@Controllerpublic class MybatisTestController {@RequestMapping(value="b-s-test",params="complex")public String getBigUser(Model model){BigUserDao dao = (BigUserDao)ApplicationContextFactory.getApplicationContext().getBean("bigUserDao");System.out.println(dao.getBigUser(new UserModel("lida1",1, 24)));model.addAttribute("message", "success");return "hello";}}
0 0
- SPRING+MYBATIS
- mybatis + spring
- mybatis + spring
- Spring MyBatis
- spring mybatis
- spring+mybatis
- spring+mybatis
- mybatis+spring
- MyBatis+Spring
- Mybatis-Spring
- spring+mybatis
- spring+mybatis
- Spring+MyBatis
- spring-mybatis
- Spring+Mybatis
- mybatis+Spring
- Mybatis+Spring
- spring-mybatis
- 为什么写测试用例?编写测试用例从何着手?测试用例包含什么内容?
- 如何向中级程序员转变
- 杭电 1860 统计字符
- 数据库创建
- 纯代码sizeclass使用
- Mybatis-Spring
- NSString / NSData / char* 类型之间的转换
- iOS_网络请求_代理方式
- To and Fro(字符串规律)
- 37. Sudoku Solver
- leason 2.CentOS切换用户
- shell脚本---mysql检测监控脚本系列
- LVS+keeplived+nginx+apache搭建高可用、高性能php集群
- java单元测试工具--JUnit