SSM-chinasoft_struts2_spring3_mybatis3

<?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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       
       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-3.0.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

    
    <!--自动扫描,并加载Bean-->
    <context:component-scan base-package="com.chinasoft" />
    
    <!-- 配置Data Source -->
    <context:property-placeholder location="classpath:config/jdbc.properties"/>
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    
    <!--
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    -->
    
    <!--配置SqlSessionFactoryBean,它是用于创建 SqlSessionFactory的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:config/sqlMapConfiguration.xml"></property>
    </bean>
    
    <!--第一种方式配置User mapper -->
    <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="mapperInterface" value="com.chinasoft.dao.IUserDao" />
        <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    -->
    
    <!--第二种方式配置User mapper,指定一个父Mapper,在其它的Maper中指定这个父Maper -->
    <!--
    <bean id="baseMapper" class="org.mybatis.spring.mapper.MapperFactoryBean" abstract="true" lazy-init="true">
      <property name="sqlSessionFactory" ref="sqlSessionFactory" />
    </bean>
    
    <bean id="userMapper" parent="baseMapper" >
        <property name="mapperInterface" value="com.chinasoft.dao.IUserDao" />
    </bean>
     -->
    
    <!-- 第二种方式配置User mapper:
        如果有多个UserMapper需要配置的时候,我们就可以配置MapperScannerConfigurer
        让它实现自动扫描文件夹的功能
     -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--<property name="sqlSessionFactory" ref="sqlSessionFactory" />
        --><property name="basePackage" value="com.chinasoft.dao" />
    </bean>
    
    <!-- 配置事务管理者 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- <1>声明使用annotation的方式来处理事务 -->
    <!--<tx:annotation-driven transaction-manager="transactionManager"/> -->
    
    <!-- <2>使用Xml的方式来处理事务,也就是Aop的声明式事务 -->
    <aop:config>
        <!-- 定义一个pointcut -->
        <aop:pointcut id="serviceTransactionBusiness" expression="execution(public * com.chinasoft.service..*.*(..))" />
        <!--建议将哪个advice加在定义的pointcut上面 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceTransactionBusiness"/>
    </aop:config>
    
    <!-- 建议的定义,声明式事务建议 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!--所有以find开头的方法都会加上read-only事务 -->
            <tx:method name="find*" read-only="true" />
            <!-- 设定以add,update,delete开头的方法都启用默认的事务 -->
            <tx:method name="add*" propagation="REQUIRED" isolation="DEFAULT"/>
            <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" />
            <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" />
        </tx:attributes>
    </tx:advice>

</beans>

applicationContext.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>
    <!--读取sqlMaper.properties配置文件-->
    <!--<properties resource="sqlMaper.properties" /> -->
    
    <typeAliases>
        <!--给实体类起一个别名 user -->
        <typeAlias type="com.chinasoft.entity.User" alias="user"/>
    </typeAliases>
    
    <!--
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
     -->
    
    <mappers>
        <!--userMapper.xml装载进来  同等于把“dao”的实现装载进来 -->
        <mapper resource="com/chinasoft/entity/UserMaper.xml" />
    </mappers>
</configuration>
--------------------------sqlMapConfiguration.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <!--<constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" namespace="/" extends="struts-default">
        <default-action-ref name="index" />
        <global-results>
            <result name="error">/error.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception" result="error"/>
        </global-exception-mappings>

        <action name="index">
            <result type="redirectAction">
                <param name="actionName">HelloWorld</param>
                <param name="namespace">/example</param>
            </result>
        </action>
    </package>
    <include file="example.xml"/>
    -->
    <!-- struts中动态的方法调用 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <!-- 开发模式设置成true表示支持热部署 -->
    <constant name="struts.devMode" value="true" />
    
    <package name="com.chinasoft.user" namespace="/" extends="struts-default">
        <action name="userAction"  class="com.chinasoft.action.UserAction">
            <result name="success">/success.jsp</result>
            <result name="failure">/failure.jsp</result>
        </action>
    </package>
</struts>

---------------struts.xml

package com.chinasoft.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.chinasoft.entity.User;
import com.chinasoft.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

@Component("userAction")
public class UserAction extends ActionSupport{

    private static final long serialVersionUID = 9160145984518262102L;
    
    @Autowired
    private IUserService userService;
    
    private User user;
    
    private List<User> userList;
    
    public String addUser(){
        boolean flag = userService.addUser(user);
        if(flag){
            userList = userService.findAllUser();
        }
        return flag==true?"success":"failure";
    }
    
    public String deleteUser(){
        boolean flag = userService.deleteUserById(user.getId());
        if(flag){
            userList = userService.findAllUser();
        }
        return flag==true?"success":"failure";
    }
    
    public String updateUser(){
        return userService.updateUser(user)==true?"success":"failure";
    }
    
    public String getAllUser(){
        userList = userService.findAllUser();
        if(null!=userList){
            user = userList.get(0);
        }
        return SUCCESS;
    }
    
    public String getUserById(){
        user = userService.findUserById(user.getId());
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUserList() {
        return userList;
    }

    public void setUserList(List<User> userList) {
        this.userList = userList;
    }
}

---------------------------------------

package com.chinasoft.dao;

import java.util.List;

import com.chinasoft.entity.User;

public interface IUserDao {

public User findUserById(int id);
    
    public List<User> findAllUser();
    
    public Integer addUser(User user);
    
    public Integer updateUser(User user);
    
    public Integer deleteUserById(int id);
}
--------------------------------------------------

package com.chinasoft.entity;

import java.io.Serializable;

public class User implements Serializable{
    
    private static final long serialVersionUID = -4971543883942732140L;

    private Integer id;
    
    private String name;
    
    private Integer sex;
    
    private String mailAddress;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getSex() {
        return sex;
    }

    public void setSex(Integer sex) {
        this.sex = sex;
    }

    public String getMailAddress() {
        return mailAddress;
    }

    public void setMailAddress(String mailAddress) {
        this.mailAddress = mailAddress;
    }
}

-----------------------------------------------

<?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">

<!--这块等于dao接口的实现  namespace必须和接口的类路径一样-->
<mapper namespace="com.chinasoft.dao.IUserDao">
    <select id="findUserById" parameterType="int" resultType="user">
        select * from T_User where id = #{id}
    </select>
    
    <select id="findAllUser" resultType="user">
        select * from T_User
    </select>
    
    <insert id="addUser" parameterType="user">
        <selectKey keyProperty="id" resultType="int" order="BEFORE">
            select T_USER_SEQ.NEXTVAL from dual
        </selectKey>
        insert into T_User(id,name,sex,mailAddress)values(#{id},#{name},#{sex},#{mailAddress})
    </insert>
    
    <update id="updateUser" parameterType="user">
        update T_User set name=#{name},sex=#{sex},mailAddress=#{mailAddress} where id=#{id}
    </update>
    
    <delete id="deleteUserById" parameterType="int">
        delete from T_User where id = #{id}
    </delete>
</mapper>
---------------------------------

package com.chinasoft.service;

import java.util.List;

import com.chinasoft.entity.User;

public interface IUserService {
    
    public User findUserById(int id);
    
    public List<User> findAllUser();
    
    public boolean addUser(User user);
    
    public boolean updateUser(User user);
    
    public boolean deleteUserById(int id);
    
    public boolean updateAndDeleteTest();
}
------------------------------------------------

package com.chinasoft.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.chinasoft.dao.IUserDao;
import com.chinasoft.entity.User;

@Service("userService")
public class UserServiceImpl implements IUserService {
    
    @Autowired
    private IUserDao userDao;

    @Override
    public User findUserById(int id) {
        return userDao.findUserById(id);
    }

    @Override
    public List<User> findAllUser() {
        return userDao.findAllUser();
    }

    @Override
    public boolean addUser(User user) {
        return userDao.addUser(user)>0?true:false;
    }

    @Override
    public boolean updateUser(User user) {
        return userDao.updateUser(user)>0?true:false;
    }

    @Override
    public boolean deleteUserById(int id) {
        return userDao.deleteUserById(id)>0?true:false;
    }

    @Override
    @Transactional
    public boolean updateAndDeleteTest() {
        
        User user = new User();
        user.setId(84);
        user.setName("Jason.chen111");
        user.setSex(0);
        user.setMailAddress("Jason.chen111@163.com");
        userDao.updateUser(user);
        
        userDao.deleteUserById(85);
        return true;
    }
}

------------------------------------------------


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值