在我的博客首页有s2sh整合的例子,现在要做的工作就是将此工程中hibernate换成mybatis,并且使用比较流行的annotation注解来实现。首先肯定是要将hibernate的jar包全部干掉,然后加上mybatis的jar包及其与spring整合所需jar包,下面只贴主要改动的类及其配置文件:
一.mybatis配置文件mybatis-config.xml
1.
在我的博客首页有s2sh整合的例子,现在要做的工作就是将此工程中hibernate换成mybatis,并且使用比较流行的annotation注解来实现。首先肯定是要将hibernate的jar包全部干掉,然后加上mybatis的jar包及其与spring整合所需jar包,下面只贴主要改动的类及其配置文件:
一.mybatis配置文件mybatis-config.xml
1. >
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
二、bean的mapper文件
Student.xml
1. version="1.0" encoding="utf-8" ?>
2. >
3.
4.
30. 31. class="com.mchange.v2.c3p0.ComboPooledDataSource"> 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64.
65. 66. class="org.mybatis.spring.SqlSessionFactoryBean"> 67. 68. value="classpath:mybatis-config.xml" /> 69. 70.
71.
72.
73.
74.
75. 76. class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 77. 78.
79.
80.
81.
82. 四、泛型基类BaseDAOImpl.java
83.
package com.anxin.dao.impl;84.
85. import java.io.Serializable;
86. import java.util.List;
87. import java.util.Map;
88.
89. import javax.annotation.Resource;
90.
91. import org.apache.ibatis.session.SqlSessionFactory;
92. import org.mybatis.spring.support.SqlSessionDaoSupport;
93. import org.slf4j.Logger;
94. import org.slf4j.LoggerFactory;
95. import org.springframework.beans.factory.annotation.Autowired;
96.
97. import com.anxin.dao.BaseDAO;
98. import com.anxin.util.PageListData;
99.
100. public class BaseDAOImpl
extends 101. SqlSessionDaoSupport implements BaseDAO
{ 102.
103. public static Logger logger = LoggerFactory.getLogger(BaseDAOImpl.class);
104. @Autowired(required = true)
105. @Resource(name = "sqlSessionFactory")
106. public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
107. super.setSqlSessionFactory(sqlSessionFactory);
108. }
109. // 保存
110. public T save(T entity) {
111. try {
112. getSqlSessionTemplate().insert(
113. entity.getClass().getSimpleName() + ".save", entity);
114. return entity;
115. } catch (RuntimeException re) {
116. logger.error("save " + entity.getClass().getName() + " failed :{}",
117. re);
118. throw re;
119. }
120. }
121. // 更新
122. public void update(T entity) {
123. try {
124. this.getSqlSessionTemplate().update(
125. entity.getClass().getSimpleName() + ".update", entity);
126. } catch (RuntimeException re) {
127. logger.error("update " + entity.getClass().getName()
128. + " failed :{}", re);
129. throw re;
130. }
131. }
132. // 删除
133. public void delete(T entity) {
134. try {
135. this.getSqlSessionTemplate().delete(
136. entity.getClass().getSimpleName() + ".delete", entity);
137. } catch (RuntimeException re) {
138. logger.error("delete " + entity.getClass().getName()
139. + " failed :{}", re);
140. throw re;
141. }
142. }
143.
144. // 根据id删除某个对象
145. public void delete(Class
entityClass, PK id) { 146. try {
147. this.getSqlSessionTemplate().delete(
148. entityClass.getSimpleName() + ".deleteById", id);
149. } catch (RuntimeException re) {
150. logger.error("delete " + entityClass.getName() + "failed :{}", re);
151. throw re;
152. }
153. }
154. // 根据id加载某个对象
155. public T findById(Class
entityClass, PK id) { 156. try {
157. return (T) this.getSqlSessionTemplate().selectOne(
158. entityClass.getSimpleName() + ".findById", id);
159. } catch (RuntimeException re) {
160. logger.error("findById " + entityClass.getName() + "failed :{}",
161. re);
162. throw re;
163. }
164. }
165.
166. // 查找所有的对象
167. public List
findAll(Class entityClass) { 168. try {
169. return this.getSqlSessionTemplate().selectList(
170. entityClass.getSimpleName() + ".findAll");
171. } catch (RuntimeException re) {
172. logger
173. .error("findAll " + entityClass.getName() + "failed :{}",
174. re);
175. throw re;
176. }
177. }
178. // 根据查询参数,当前页数,每页显示的数目得到分页列表
179. public PageListData queryPage(Class
entityClass, Map param, 180. int currentPage, int pageSize) {
181. try {
182. return new PageListData((Integer)getSqlSessionTemplate()
183. .selectOne(entityClass.getSimpleName()+".getTotalCounts",param), pageSize, currentPage, this
184. .getSqlSessionTemplate().selectList(
185. entityClass.getSimpleName() + ".queryPage", param));
186. } catch (RuntimeException re) {
187. logger.error("findList " + entityClass.getName() + "failed :{}",
188. re);
189. throw re;
190. }
191. }
192. }
193.
194.
195.
196.
197.
198.
199. StudentDAOImpl.java
200.
201.
202.
203.
204.
package com.anxin.dao.impl;205.
206. import org.springframework.stereotype.Repository;
207.
208. import com.anxin.bean.Student;
209. import com.anxin.dao.StudentDAO;
210.
211. @Repository
212. public class StudentDAOImpl extends BaseDAOImpl
implements StudentDAO { 213. }
214.
215.
216.
217.
218.
219.
220.
mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
二、bean的mapper文件
Student.xml
1. version="1.0" encoding="utf-8" ?>
2. >
3.
4.
30. 31. class="com.mchange.v2.c3p0.ComboPooledDataSource"> 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64.
65. 66. class="org.mybatis.spring.SqlSessionFactoryBean"> 67. 68. value="classpath:mybatis-config.xml" /> 69. 70.
71.
72.
73.
74.
75. 76. class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 77. 78.
79.
80.
81.
82. 四、泛型基类BaseDAOImpl.java
83.
package com.anxin.dao.impl;84.
85. import java.io.Serializable;
86. import java.util.List;
87. import java.util.Map;
88.
89. import javax.annotation.Resource;
90.
91. import org.apache.ibatis.session.SqlSessionFactory;
92. import org.mybatis.spring.support.SqlSessionDaoSupport;
93. import org.slf4j.Logger;
94. import org.slf4j.LoggerFactory;
95. import org.springframework.beans.factory.annotation.Autowired;
96.
97. import com.anxin.dao.BaseDAO;
98. import com.anxin.util.PageListData;
99.
100. public class BaseDAOImpl
extends 101. SqlSessionDaoSupport implements BaseDAO
{ 102.
103. public static Logger logger = LoggerFactory.getLogger(BaseDAOImpl.class);
104. @Autowired(required = true)
105. @Resource(name = "sqlSessionFactory")
106. public void setSuperSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
107. super.setSqlSessionFactory(sqlSessionFactory);
108. }
109. // 保存
110. public T save(T entity) {
111. try {
112. getSqlSessionTemplate().insert(
113. entity.getClass().getSimpleName() + ".save", entity);
114. return entity;
115. } catch (RuntimeException re) {
116. logger.error("save " + entity.getClass().getName() + " failed :{}",
117. re);
118. throw re;
119. }
120. }
121. // 更新
122. public void update(T entity) {
123. try {
124. this.getSqlSessionTemplate().update(
125. entity.getClass().getSimpleName() + ".update", entity);
126. } catch (RuntimeException re) {
127. logger.error("update " + entity.getClass().getName()
128. + " failed :{}", re);
129. throw re;
130. }
131. }
132. // 删除
133. public void delete(T entity) {
134. try {
135. this.getSqlSessionTemplate().delete(
136. entity.getClass().getSimpleName() + ".delete", entity);
137. } catch (RuntimeException re) {
138. logger.error("delete " + entity.getClass().getName()
139. + " failed :{}", re);
140. throw re;
141. }
142. }
143.
144. // 根据id删除某个对象
145. public void delete(Class
entityClass, PK id) { 146. try {
147. this.getSqlSessionTemplate().delete(
148. entityClass.getSimpleName() + ".deleteById", id);
149. } catch (RuntimeException re) {
150. logger.error("delete " + entityClass.getName() + "failed :{}", re);
151. throw re;
152. }
153. }
154. // 根据id加载某个对象
155. public T findById(Class
entityClass, PK id) { 156. try {
157. return (T) this.getSqlSessionTemplate().selectOne(
158. entityClass.getSimpleName() + ".findById", id);
159. } catch (RuntimeException re) {
160. logger.error("findById " + entityClass.getName() + "failed :{}",
161. re);
162. throw re;
163. }
164. }
165.
166. // 查找所有的对象
167. public List
findAll(Class entityClass) { 168. try {
169. return this.getSqlSessionTemplate().selectList(
170. entityClass.getSimpleName() + ".findAll");
171. } catch (RuntimeException re) {
172. logger
173. .error("findAll " + entityClass.getName() + "failed :{}",
174. re);
175. throw re;
176. }
177. }
178. // 根据查询参数,当前页数,每页显示的数目得到分页列表
179. public PageListData queryPage(Class
entityClass, Map param, 180. int currentPage, int pageSize) {
181. try {
182. return new PageListData((Integer)getSqlSessionTemplate()
183. .selectOne(entityClass.getSimpleName()+".getTotalCounts",param), pageSize, currentPage, this
184. .getSqlSessionTemplate().selectList(
185. entityClass.getSimpleName() + ".queryPage", param));
186. } catch (RuntimeException re) {
187. logger.error("findList " + entityClass.getName() + "failed :{}",
188. re);
189. throw re;
190. }
191. }
192. }
193.
194.
195.
196.
197.
198.
199. StudentDAOImpl.java
200.
201.
202.
203.
204.
package com.anxin.dao.impl;205.
206. import org.springframework.stereotype.Repository;
207.
208. import com.anxin.bean.Student;
209. import com.anxin.dao.StudentDAO;
210.
211. @Repository
212. public class StudentDAOImpl extends BaseDAOImpl
implements StudentDAO { 213. }
214.
215.
216.
217.
218.
219.
220.