带着纠结和郁闷终于把spring3.x和mybatis3.x的整合给配成功了!
乱七八糟的文档,和乱七八糟的demo,刚开始发现 官方sping 和 mybatis 两个没有与spring 和hibernate 的文档那么详细,去看sping的文档 ,NND 的还是 ibatis 的 。艹。
下面把 demo 列出来:
先把类 弄出来:
package model;
public class User {
private int id;
private String name; private String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package model.mapper;
public interface UserMapper {
public User selectOne(int id); }
把测试的类也先弄出来:
@Test public void testSuccess() { ApplicationContext ac = new ClassPathXmlApplicationContext( new String[]{"classpath*:resource/spring.xml"}); UserMapper u = (UserMapper)ac.getBean("userMapper"); User user = u.selectOne(3); System.out.println(user.getName()); }
以上三个类相信没啥问题。 下篇文章,会对几个类进行详细的分析,这就不多介绍了。
下面是配置文件
sping.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath:resource/jdbc.properties</value> </property> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="${jdbc.driverClassName}" /> <property name="jdbcUrl" value="${jdbc.url}" /> <property name="user" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="resource/mybatis-config.xml" /> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> <property name="mapperInterface" value=" model.mapper.UserMapper" /> </bean> </beans>
这里有个 jdbc.properties 配置 建议去看sping 的文档 3.4 小节, 很简单哈 。
mybatis-config.xml :
<?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> <mapper resource="model/mapper/User.xml"/> </mappers> </configuration>
User.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="model.mapper.UserMapper"> <resultMap type="User" id="getUser"> <result property="id" column="id" /> <result property="name" column="username" /> <result property="password" column="password" /> </resultMap> <select id="selectOne" parameterType="Integer" resultMap="getUser"> select * from user where id = #{id} </select> </mapper>
差不多了,大概就这些 。
架包乱七八糟的加了一通 , sping 3.X 下面的 差不多 都给 弄进来 呵呵 , 刚开始就没管那么多了,先把程序弄起来再说吧。看附件图片哈
上面demo 整理 过程 不是很规范 。 应该有点问题。
感觉 两者还是这么着没啥 特别的地方,sping 基本的 了解哈 , 不难理解。