手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld

本教程只是一个小小的入门,几个技术的整合并没有想象中的繁琐和难度。
项目结构浏览。
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第1张图片
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第2张图片
一、jar 包准备。
(1)Spring4 、SpringMVC 的基本 jar 包
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第3张图片
(2)MyBatis 的 jar 包

mybatis-3.2.4.jar

(3)Spring 与 MyBatis 整合的 jar 包

mybatis-spring-1.2.0.jar

(4)MySQL 驱动 jar 包

mysql-connector-java-5.1.29.jar

(5)AOP 实现类的 jar 包

aopalliance.jar
aspectjrt.jar
aspectjweaver.jar

(6)log4j 的 jar 包

log4j-1.2.17.jar

二、新建一个 Dynamic Web项目,配置 web.xml 文件
(1)配置 Spring4 的监听器

    
    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:applicationContext.xmlparam-value>
    context-param>
    
    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

(2)配置字符编码过滤器

    
    <filter>
        <filter-name>encodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>encodingFilterfilter-name>
        <url-pattern>/*url-pattern>
    filter-mapping>

(3)配置 SpringMVC 的过滤器

    
    
    <servlet>
        <servlet-name>springDispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:spring-mvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
        
    servlet>

    
    <servlet-mapping>
        <servlet-name>springDispatcherServletservlet-name>
        <url-pattern>*.dourl-pattern>
    servlet-mapping>

三、添加各种配置文件
(1) SpringMVC 配置文件 spring-mvc.xml
只用 beans 和 context 的约束。
代码:


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

    
    <context:component-scan base-package="com.liwei.controller">context:component-scan>

    
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/user">property>
        <property name="suffix" value=".jsp">property>
    bean>

beans>

(2)MyBatis 的配置文件 mybatis-config.xml
只用配置类的别名即可。
代码:



<configuration>
    <typeAliases>
        <package name="com.liwei.entities"/>
    typeAliases>
configuration>

(3) log4j 日志组件配置文件 log4j.properties
(以下仅供参考,可以自己实现更个性化的配置)
代码:

log4j.rootLogger=DEBUG,appender1,appender2

log4j.appender.appender1=org.apache.log4j.ConsoleAppender 

log4j.appender.appender2=org.apache.log4j.FileAppender 
log4j.appender.appender2.File=D:/logFile.txt

log4j.appender.appender1.layout=org.apache.log4j.PatternLayout
log4j.appender.appender2.layout=org.apache.log4j.TTCCLayout  

# liwei 添加
log4j.logger.com.ibatis = DEBUG  
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG  
log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG  
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG  
log4j.logger.java.sql.Connection = DEBUG  
log4j.logger.java.sql.Statement = DEBUG  
log4j.logger.java.sql.PreparedStatement = DEBUG 
log4j.logger.java.sql.ResultSet = DEBUG  

(4)Spring4 的核心配置文件(重要):applicationContext.xml
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第4张图片
代码:


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

    
    <context:component-scan base-package="com.liwei.dao">context:component-scan>
    <context:component-scan base-package="com.liwei.service">context:component-scan>

    
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/db_mybatis"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource">property>
        
        <property name="mapperLocations" value="classpath:com/liwei/mappers/*.xml">property>
        
        <property name="configLocation" value="classpath:mybatis-config.xml">property>
    bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.liwei.dao">property>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
    bean>

    
    
    
    
    
    

    
    <bean id="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="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="edit*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED" />  
            <tx:method name="add*" propagation="REQUIRED" />  
            <tx:method name="new*" propagation="REQUIRED" />  
            <tx:method name="set*" propagation="REQUIRED" />  
            <tx:method name="remove*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="change*" propagation="REQUIRED" />  
            <tx:method name="get*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="load*" propagation="REQUIRED" read-only="true" />  
            <tx:method name="*" propagation="REQUIRED" read-only="true" />  
        tx:attributes>  
    tx:advice>  


    
    <aop:config>
        <aop:pointcut expression="execution(* com.liwei.service.*.*(..))" id="serviceOperation"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
    aop:config>

beans>

此时,环境搭建已经大功告成。可以开始写我们的业务代码了。
四、业务代码简介
1、实体类
这里写图片描述
2、dao 层接口
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第5张图片
3、mapper 映射配置文件
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第6张图片
4、Service 接口和实现类
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第7张图片
5、Controller 控制器
手把手 Spring、SpringMVC、MyBatis 整合教程之 HelloWorld_第8张图片
6、前台页面
(1)登录页面
这里写图片描述
(2)成功页面
这里写图片描述
六、其他
(1)SQL 建表语句

DROP TABLE IF EXISTS `t_user`;

CREATE TABLE `t_user` (
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `userName` VARCHAR(20) DEFAULT NULL,
  `password` VARCHAR(20) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=INNODB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;



INSERT  INTO `t_user`(`id`,`userName`,`password`) VALUES (1,'liwei','123456');

(2)测试访问链接

http://localhost:8080/MyBatisWeb/user/login.do

以下是一份比较完整地整合教程:
1、框架搭建:加入了 maven 项目管理
http://blog.csdn.net/zhshulin/article/details/37956105
2、加入了注解管理事务
http://blog.csdn.net/zhshulin/article/details/38108159

你可能感兴趣的:(spring4,springmvc,mybatis)