SSM框架整合

SSM
S:SpringMVC S:Spring M:MyBatis
项目结构:
这里写图片描述
所需包:

aopalliance-1.0.jar
asm-3.3.1.jar
aspectjweaver-1.6.11.jar
c3p0-0.9.1.2.jar
cglib-2.2.2.jar
commons-dbcp-1.2.2.jar
commons-logging-1.1.1.jar
commons-pool-1.3.jar
javassist-3.17.1-GA.jar
jsp-api.jar
jstl-1.2.jar
junit-4.9.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.2.jar
mysql-connector-java-5.1.7-bin.jar
servlet-api.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.3.7.RELEASE.jar
spring-aspects-4.3.7.RELEASE.jar
spring-beans-4.3.7.RELEASE.jar
spring-context-4.3.7.RELEASE.jar
spring-core-4.3.7.RELEASE.jar
spring-expression-4.3.7.RELEASE.jar
spring-instrument-4.3.7.RELEASE.jar
spring-jdbc-4.3.7.RELEASE.jar
spring-orm-4.3.7.RELEASE.jar
spring-test-4.3.7.RELEASE.jar
spring-tx-4.3.7.RELEASE.jar
spring-web-4.3.7.RELEASE.jar
spring-webmvc-4.3.7.RELEASE.jar

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssm_model</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 配置SpringMVC-->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:config/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <servlet-mapping>
        <servlet-name>springDispatcherServlet</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>


    <!-- 配置Spring -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/classes/config/bean-*.xml</param-value>
    </context-param>

    <!-- 配置Spring的监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener> 
</web-app>

bean-base.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
      <property name="acquireIncrement" value="2"></property>
      <property name="maxPoolSize" value="100"></property>
      <property name="minPoolSize" value="2"></property>
      <property name="maxStatements" value="100"></property>
      <property name="jdbcUrl" value="jdbc:mysql:///ibatis001"></property>
      <property name="user" value="root"></property>
      <property name="password" value="123456"></property>
      <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    </bean>

    <!--iBatis和Spring整合的关键点  SqlSessionfactory的创建和事物的管理-->
    <bean id="sqlSessionFactory"    class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--配置iBatis的一个扫描器-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="beanName" value="sqlSessionFactory"></property>
      <!--配置的是mapper的扫描包-->
      <property name="basePackage" value="com.wc.mapper"></property>
    </bean>

    <!--配置的是Spring的扫描器-->
    <context:component-scan base-package="com.wc"></context:component-scan>
    <!--配置aop的注解扫描-->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy> 

</beans>        

spring-mvc.xml

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd">
        <!--配置springMVC的注解扫描-->
    <context:component-scan base-package="com.wc"></context:component-scan>
    <!--配置mvc的注解驱动-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

User.java

public class User {

    private int uId;
    private String uName;
    private String uPassword;
    public int getuId() {
        return uId;
    }
    public void setuId(int uId) {
        this.uId = uId;
    }
    public String getuName() {
        return uName;
    }
    public void setuName(String uName) {
        this.uName = uName;
    }
    public String getuPassword() {
        return uPassword;
    }
    public void setuPassword(String uPassword) {
        this.uPassword = uPassword;
    }
    @Override
    public String toString() {
        return "User [uId=" + uId + ", uName=" + uName + ", uPassword="
                + uPassword + "]";
    }
}

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.wc.mapper.UserMapper">
  <!--向数据库添加数据-->
  <insert id="add" parameterType="com.wc.pojo.User">
    insert into t_user(uName,uPassword) values(#{uName},#{uPassword})
  </insert>
</mapper> 

UserMapper.java


public interface UserMapper {
    /**
     * 向数据库添加数据
     * @param user
     */
    void add(User user);
}

IUserSerivce.java

public interface IUserSerivce {
    /**
     * 注册用户
     * @param user
     */
    void register(User user);
}

UserService.java

@Service
public class UserService implements IUserSerivce{
    @Autowired
    private  UserMapper userMapper=null;
    @Override
    public void register(User user) {
        userMapper.add(user);
    }
}

UserControll.java

@Controller
public class UserControll {
    @Autowired
    private IUserSerivce userSerivce=null;
    @RequestMapping("/hello.action")
    public String regsiter(){
        User user=new User();
        user.setuName("qwertyu");
        user.setuPassword("123456");
        userSerivce.register(user);
        return "/index.jsp";    
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值