原文: http://blog.csdn.net/sustbeckham/archive/2010/12/17/6082677.aspx
mybatis一直没有发布release版本。所以spring也坐着看。但是spring还是必须用啊。
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">
-
-
-
-
- <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;
- }
-
- @Override
- public 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"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource">
- <property name="driverClassName">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="url">
- <value>jdbc:mysql://localhost:3306/forum</value>
- </property>
- <property name="username">
- <value>root</value>
- </property>
- <property name="password">
- <value>123456</value>
- </property>
- </bean>
-
-
-
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="configLocation" value="classpath:Configuration.xml"></property>
- </bean>
-
-
-
- <bean id="peopleMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
- <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
-
- <property name="mapperInterface" value="cn.java.forum.domain.mapper.PeopleMapper"/>
- </bean>
-
-
- <bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" 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>
-
-
- <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" class="cn.java.forum.domain.service.impl.PeopleServiceBean">
- <property name="mapper">
- <ref bean="peopleMapper"/>
- </property>
- </bean>
- </beans>
4.最后,是启动spring的配置 WEB.XML
- <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来测试(其实类也可以的。)。
- package servlet;
-
- import java.io.IOException;
- import java.io.PrintWriter;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- import cn.java.forum.domain.service.PeopleService;
-
- public class TestServlet extends HttpServlet {
-
- private static final long serialVersionUID = 1L;
-
- public TestServlet() {
- super();
- }
-
- public void destroy() {
- super.destroy();
- }
-
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- doPost(request, response);
- }
-
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
-
- response.setContentType("text/html");
- PrintWriter out = response.getWriter();
-
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- PeopleService peopleService = (PeopleService) ctx.getBean("peopleService");
- System.out.println("the list:"+peopleService.allPeople());
- out.flush();
- out.close();
- }
-
- public void init() throws ServletException {
- }
-
- }
控制台显示 ....................
最后一行。
OK 大功告成。