首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

Spring3.05简略集成MyBatis3.03

2012-11-21 
Spring3.05简单集成MyBatis3.031. Pojo & mapper配置?package cn.java.forum.domainimport java.io.Seria

Spring3.05简单集成MyBatis3.03

1. Pojo & mapper配置

?

package cn.java.forum.domain;import java.io.Serializable;import java.util.Date;public class People implements Serializable{ private static final long serialVersionUID = 1L;private int id; private String username; private String password; private String realName; private Date registerTime; @Override public String toString() { return "< id:"+id+", username:"+username+", password:"+password+", realName:"+realName +", registerTime:"+registerTime.toLocaleString()+" >"; } public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getRealName() {return realName;}public void setRealName(String realName) {this.realName = realName;}public Date getRegisterTime() {return registerTime;}public void setRegisterTime(Date registerTime) {this.registerTime = registerTime;}}

<?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="cn.java.forum.domain.mapper.PeopleMapper"> <!-- <cache eviction="FIFO" flushInterval="30000" readOnly="true" size="512"></cache> --> <select id="allPeople" resultType="People"> select * from People </select></mapper>

2. mapper接口

package cn.java.forum.domain.mapper;import java.util.List;import cn.java.forum.domain.People;public interface PeopleMapper { List<People> allPeople();}

3.service接口

package cn.java.forum.domain.service;import java.util.List;import cn.java.forum.domain.People;public interface PeopleService {List<People> allPeople();}

4. service实现,mapper经过注入。

package cn.java.forum.domain.service.impl;import java.util.List;import cn.java.forum.domain.People;import cn.java.forum.domain.mapper.PeopleMapper;import cn.java.forum.domain.service.PeopleService;public class PeopleServiceBean implements PeopleService{/* 需要注入 */private PeopleMapper mapper;public PeopleMapper getMapper() {return mapper;}public void setMapper(PeopleMapper mapper) {this.mapper = mapper;}@Overridepublic List<People> allPeople() {return mapper.allPeople();}}

基本的代码完成之后。就是配置文件了。

---------------------------------------------------华丽的分割线---------------------------------------------------

1.完整的spring主配置文件,包含了事务,mapper,数据源等的配置

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- ============================== 数据库配置 ==================================== --><!-- 数据源配置 --><bean name="dataSource"ref="dataSource" /><property name="configLocation" value="classpath:Configuration.xml"></property></bean> <!-- ================================= mapper ============================================= --><!-- 人员mapper --><bean id="peopleMapper" ref="sqlSessionFactory"/><!-- mapper的位置 --><property name="mapperInterface" value="cn.java.forum.domain.mapper.PeopleMapper"/></bean><!-- ================================= 事务控制相关 ============================================= --> <bean name="transactionManager" ref="dataSource"></property> </bean> <tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="delete*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" no-rollback-for="java.lang.RuntimeException"/><tx:method name="insert*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.RuntimeException" /><tx:method name="update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" /><tx:method name="find*" propagation="SUPPORTS"/><tx:method name="get*" propagation="SUPPORTS"/><tx:method name="select*" propagation="SUPPORTS"/></tx:attributes></tx:advice> <!-- 引入service层的spring配置 --><import resource="applicationContext-service.xml"/> </beans>

2.MyBatis的配置文件,暂时只配置了 别名 和 mapper

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="cn.java.forum.domain.Grade" alias="Grade"/> <typeAlias type="cn.java.forum.domain.People" alias="People"/> <typeAlias type="cn.java.forum.domain.Response" alias="Response"/> <typeAlias type="cn.java.forum.domain.Topic" alias="Topic"/> </typeAliases> <!-- spring配置之后 这些就可以省略了<environments default="development"><environment id="development"><transactionManager type="JDBC"></transactionManager><dataSource type="POOLED"><property name="driver"value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql://localhost:3306/forum" /><property name="username" value="root" /><property name="password" value="123456" /></dataSource></environment></environments> --> <mappers> <mapper resource="cn/java/forum/domain/config/Grade.xml"/> <mapper resource="cn/java/forum/domain/config/People.xml"/> <mapper resource="cn/java/forum/domain/config/Response.xml"/> <mapper resource="cn/java/forum/domain/config/Topic.xml"/> </mappers></configuration>

3. service层的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <bean id="peopleService" name="code"><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

ok? 写一个servlet来测试(其实类也可以的。)。

view plaincopy to clipboardprint?
  1. package?servlet;????
  2. import?java.io.IOException;??import?java.io.PrintWriter;??
  3. ??import?javax.servlet.ServletException;??
  4. import?javax.servlet.http.HttpServlet;??import?javax.servlet.http.HttpServletRequest;??
  5. import?javax.servlet.http.HttpServletResponse;????
  6. import?org.springframework.context.ApplicationContext;??import?org.springframework.context.support.ClassPathXmlApplicationContext;??
  7. ??import?cn.java.forum.domain.service.PeopleService;??
  8. ??public?class?TestServlet?extends?HttpServlet?{??
  9. ??????private?static?final?long?serialVersionUID?=?1L;??
  10. ??????public?TestServlet()?{??
  11. ????????super();??????}??
  12. ??????public?void?destroy()?{??
  13. ????????super.destroy();??????}??
  14. ??????public?void?doGet(HttpServletRequest?request,?HttpServletResponse?response)??
  15. ????????????throws?ServletException,?IOException?{??????????doPost(request,?response);??
  16. ????}????
  17. ????public?void?doPost(HttpServletRequest?request,?HttpServletResponse?response)??????????????throws?ServletException,?IOException?{??
  18. ??????????response.setContentType("text/html");??
  19. ????????PrintWriter?out?=?response.getWriter();?????????????
  20. ????????ApplicationContext?ctx?=?new?ClassPathXmlApplicationContext("applicationContext.xml");??????????PeopleService?peopleService?=?(PeopleService)?ctx.getBean("peopleService");??
  21. ????????System.out.println("the?list:"+peopleService.allPeople());??????????out.flush();??
  22. ????????out.close();??????}??
  23. ??????public?void?init()?throws?ServletException?{??
  24. ????}????
  25. }?

热点排行