spring3+mybatis整合事例ITeye - 娱乐之横扫全球

spring3+mybatis整合事例ITeye

2019-01-12 04:17:11 | 作者: 哲瀚 | 标签: 装备,业务,二级缓存 | 浏览: 1127

pom.xml
 project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" 
 modelVersion 4.0.0 /modelVersion 
 groupId com.chen /groupId 
 artifactId springmvc-mybatis-demo01 /artifactId 
 version 0.0.1-SNAPSHOT /version 
 packaging war /packaging 
 build 
 defaultGoal compile /defaultGoal 
 /build 
 properties 
 spring.version 3.1.2.RELEASE /spring.version 
 /properties 
 dependencies 
 !-- servlet -- 
 dependency 
 groupId javax.servlet /groupId 
 artifactId javax.servlet-api /artifactId 
 version 3.0.1 /version 
 /dependency 
 dependency 
 groupId jstl /groupId 
 artifactId jstl /artifactId 
 version 1.2 /version 
 /dependency 
 dependency 
 groupId javax.servlet.jsp /groupId 
 artifactId jsp-api /artifactId 
 version 2.1 /version 
 scope provided /scope 
 /dependency 
 !-- spring -- 
 dependency 
 groupId org.springframework /groupId 
 artifactId spring-context-support /artifactId 
 version ${spring.version} /version 
 /dependency 
 dependency 
 groupId org.springframework /groupId 
 artifactId spring-webmvc /artifactId 
 version ${spring.version} /version 
 /dependency 
 dependency 
 groupId org.springframework /groupId 
 artifactId spring-orm /artifactId 
 version ${spring.version} /version 
 /dependency 
 !-- aop -- 
 dependency 
 groupId org.aspectj /groupId 
 artifactId aspectjweaver /artifactId 
 version 1.8.4 /version 
 /dependency 
 !-- mybatis-spring -- 
 dependency 
 groupId org.mybatis /groupId 
 artifactId mybatis-spring /artifactId 
 version 1.2.2 /version 
 /dependency 
 !-- mybatis -- 
 dependency 
 groupId org.mybatis /groupId 
 artifactId mybatis /artifactId 
 version 3.2.8 /version 
 /dependency 
 dependency 
 groupId asm /groupId 
 artifactId asm-commons /artifactId 
 version 3.3.1 /version 
 /dependency 
 !-- 二级缓存ehcache -- 
 dependency 
 groupId net.sf.ehcache /groupId 
 artifactId ehcache /artifactId 
 version 2.9.0 /version 
 /dependency 
 !-- dbcp -- 
 dependency 
 groupId commons-dbcp /groupId 
 artifactId commons-dbcp /artifactId 
 version 1.4 /version 
 /dependency 
 !-- mysql衔接 -- 
 dependency 
 groupId mysql /groupId 
 artifactId mysql-connector-java /artifactId 
 version 5.1.34 /version 
 /dependency 
 !-- slf4j-log4j -- 
 dependency 
 groupId org.slf4j /groupId 
 artifactId slf4j-api /artifactId 
 version 1.7.2 /version 
 /dependency 
 dependency 
 groupId org.slf4j /groupId 
 artifactId slf4j-log4j12 /artifactId 
 version 1.7.2 /version 
 /dependency 
 dependency 
 groupId log4j /groupId 
 artifactId log4j /artifactId 
 version 1.2.17 /version 
 /dependency 
 !-- jackson -- 
 dependency 
 groupId com.fasterxml.jackson.core /groupId 
 artifactId jackson-databind /artifactId 
 version 2.2.3 /version 
 /dependency 
 dependency 
 groupId junit /groupId 
 artifactId junit /artifactId 
 version 4.11 /version 
 /dependency 
 /dependencies 
 /project 


springMVC-servlet.xml
 ?xml version="1.0" encoding="UTF-8"? 
 beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 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.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" 
 context:component-scan base-package="com.chen.controller" /context:component-scan 
 bean 
 property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /property 
 property name="prefix" value="/WEB-INF/pages/" / 
 property name="suffix" value=".jsp" / 
 /bean 
 mvc:annotation-driven /mvc:annotation-driven 
 /beans 


applicationContext.xml
 ?xml version="1.0" encoding="UTF-8"? 
 beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:mvc="http://www.springframework.org/schema/mvc"
 xmlns:aop="http://www.springframework.org/schema/aop" 
 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.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
 context:component-scan base-package="com.chen" /context:component-scan 
 !-- 加载装备文件 -- 
 context:property-placeholder location="classpath:db.properties"/ 
 !-- 装备数据源 -- 
 bean id="dataSource" 
 property name="driverClassName" value="${db.driver}" / 
 property name="url" value="${db.url}" / 
 property name="username" value="${db.username}" / 
 property name="password" value="${db.password}" / 
 property name="maxActive" value="${connection.maxActive}" /property 
 property name="maxIdle" value="${connection.maxIdle}" /property 
 property name="minIdle" value="${connection.minIdle}" /property 
 property name="maxWait" value="${connection.maxWait}" /property 
 /bean 
 !-- 装备sessionFactory -- 
 bean id="sqlSessionFactory" 
 property name="dataSource" ref="dataSource" / 
 property name="mapperLocations" value="classpath:mapper/*.xml" /property 
 /bean 
 !-- 装备mapper即dao层 -- 
 bean 
 property name="basePackage" value="com.chen.dao" / 
 property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" / 
 /bean 
 !-- 装备业务 -- 
 bean id="transactionManager" 
 property name="dataSource" ref="dataSource" /property 
 /bean 
 !-- 装备业务增强处理 -- 
 tx:advice id="txAdvice" transaction-manager="transactionManager" 
 tx:attributes 
 tx:method name="find*" propagation="REQUIRED" read-only="true"/ 
 tx:method name="save*" propagation="REQUIRED"/ 
 tx:method name="delete*" propagation="REQUIRED"/ 
 tx:method name="*"/ 
 /tx:attributes 
 /tx:advice 
 !-- 使用业务 -- 
 aop:config 
 aop:advisor advice-ref="txAdvice" pointcut="execution(* com.chen.service..*.*(..))"/ 
 /aop:config 
 !-- 可通过注解操控业务 -- 
 tx:annotation-driven / 
 /beans 


UserMapper.xml
 ?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.chen.dao.UserMapper" 
 select id="findUserById" parameterType="long" resultType="com.chen.pojo.User" 
 select * from t_user where id = #{id}
 /select 
 /mapper 


pojo
package com.chen.pojo;
public class User {
 private Long id ;
 private String username; 
 private String address;
 public Long getId() {
 return id;
 public void setId(Long id) {
 this.id = id;
 public String getUsername() {
 return username;
 public void setUsername(String username) {
 this.username = username;
 public String getAddress() {
 return address;
 public void setAddress(String address) {
 this.address = address;


dao
package com.chen.dao;
import com.chen.pojo.User;
public interface UserMapper {
 public User findUserById(Long id);


service
package com.chen.service;
import com.chen.pojo.User;
public interface UserService { 
 public User getUser(Long id);                          
			
版权声明
本文来源于网络,版权归原作者所有,其内容与观点不代表娱乐之横扫全球立场。转载文章仅为传播更有价值的信息,如采编人员采编有误或者版权原因,请与我们联系,我们核实后立即修改或删除。

猜您喜欢的文章