spring、springmvc、mybatis整合配置(注解)
2017-10-20 16:01
579 查看
最近整合ssm框架费了不少功夫,所以,把详细配置过程列出来,不足的地方望大侠们指正!
项目目录:
![](https://img-blog.csdn.net/20171020160807649?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMjUzMTMxODc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
1.web.xml文件配置:
2.springmvc.xml文件配置:
3.spring-mybatis.xml配置
4.测试:
<1>login.jsp(登录页面)
<2>UserController控制层(userController注入的是IUserService接口,这里不能是实体类UserServiceImpl哦,
这是spring的代理模式决定的,具体看,http://blog.csdn.net/jyp360/article/details/70215002):
<3>IUserService
<4>UserServiceImpl
<5>IUserDao
<6>UserMapper.xml(值得注意的是,这里的namespace="com.cn.su.dao.IUserDao"注意哦,是以dao接口文件
包取值的,如果不这样写,会报错,详情见:http://blog.csdn.net/u010504064/article/details/47337307)
最后跳转成功后页面:success.jsp配置在WEB-INF/pages/succes.jsp,路径不要写错了。整合jar包我分享下,
这里有些jar包不需要用到,用到的都在里面。附加上链接地址:
http://download.csdn.net/download/qq_25313187/10032339
项目目录:
1.web.xml文件配置:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssm</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring-mybatis.xml</param-value> </context-param> <!-- 配置spring监听器 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 配置分发请求的前端控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- 配置编码过滤器 --> <filter> <filter-name>encode</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>forceEncoding</param-name> <param-value>True</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encode</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
2.springmvc.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:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd"> <!-- 配置控制层扫描包路径 --> <context:component-scan base-package="com.cn.su.controller"></context:component-scan> <!-- 配置视图层跳转 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/pages/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 静态资源配置 --> <mvc:annotation-driven /><!-- 启动注解,配置静态资源必须 --> <mvc:resources location="/" mapping="/*.html" /> <mvc:resources location="/css/" mapping="/css/*" /> <mvc:resources location="/js/" mapping="/js/*" /> <mvc:resources location="/images/" mapping="/images/*" /> </beans>
3.spring-mybatis.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:xsi="http://www.w3.org/2001/XMLSchema-instance" 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.1.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 "> <!-- 自动扫描包 --> <context:component-scan base-package="com.cn.su"></context:component-scan> <!-- 引入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties" /> </bean> <!-- dataSource数据源配置 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${driver}" /> <property name="url" value="${url}" /> <property name="username" value="${username}" /> <property name="password" value="${password}" /> <!-- 初始化连接大小 --> <property name="initialSize" value="${initialSize}"></property> <!-- 连接池最大数量 --> <property name="maxActive" value="${maxActive}"></property> <!-- 连接池最大空闲 --> <property name="maxIdle" value="${maxIdle}"></property> <!-- 连接池最小空闲 --> <property name="minIdle" value="${minIdle}"></property> <!-- 获取连接最大等待时间 --> <property name="maxWait" value="${maxWait}"></property> </bean> <!-- sessionFactory工厂配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- mybatis和spring完美配置,不需要配置mapper映射文件 --> <property name="mapperLocations" value="classpath*:com/cn/su/mapper/*.xml"></property> </bean> <!-- dao层配置 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- spring会自动查找该包下的类 --> <property name="basePackage" value="com.cn.su.dao" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事物的传播性 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <!-- REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务; SUPPORTS:支持当前事务,如果当前没有事务,就以非事务的方式执行 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> <tx:method name="query*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- 配置aop切面 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.cn.su.service.impl.*.*(..))"/> </aop:config> </beans>
4.测试:
<1>login.jsp(登录页面)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>登录</title> </head> <body> <h2>Login</h2> <form action="user/checkLogin" method="post"> username:<input type="text" name="username" value="" placeholder="please enter your username!"/><br> password:<input type="password" name="password" value="" placeholder="please enter your password!" /><br> <input type="submit" value="登录" /> </form> <div> <span style="color:red">${errorMessage}</span> </div> </body> </html>
<2>UserController控制层(userController注入的是IUserService接口,这里不能是实体类UserServiceImpl哦,
这是spring的代理模式决定的,具体看,http://blog.csdn.net/jyp360/article/details/70215002):
package com.cn.su.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import com.cn.su.entity.User; import com.cn.su.service.IUserService; @Controller @RequestMapping("/user") public class UserController { @Autowired private IUserService iUserService; @RequestMapping("/checkLogin") public String login(User user,Model model){ System.out.println("username:"+user.getUsername()+",password:"+user.getPassword()); boolean flag = iUserService.selectSingleUser(user.getUsername(), user.getPassword()); if(flag){ return "success"; }else{ model.addAttribute("errorMessage", "用户名或者密码不正确"); return "login"; } } }
<3>IUserService
package com.cn.su.service; public interface IUserService { public boolean selectSingleUser(String username,String password); }
<4>UserServiceImpl
package com.cn.su.service.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.cn.su.dao.IUserDao; import com.cn.su.service.IUserService; @Service public class UserServiceImpl implements IUserService{ @Autowired private IUserDao iUserDao; @Override public boolean selectSingleUser(String username,String password) { return iUserDao.selectSingleUser(username,password) == 1 ? true : false; } }
<5>IUserDao
package com.cn.su.dao; import org.apache.ibatis.annotations.Param; public interface IUserDao { public int selectSingleUser(@Param("username") String username,@Param("password") String password); }
<6>UserMapper.xml(值得注意的是,这里的namespace="com.cn.su.dao.IUserDao"注意哦,是以dao接口文件
包取值的,如果不这样写,会报错,详情见:http://blog.csdn.net/u010504064/article/details/47337307)
<?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.cn.su.dao.IUserDao"> <select id="selectSingleUser" resultType="int" parameterType="com.cn.su.entity.User"> select EXISTS(select * from tb_user where username = #{username} and password = #{password}); </select> </mapper>
最后跳转成功后页面:success.jsp配置在WEB-INF/pages/succes.jsp,路径不要写错了。整合jar包我分享下,
这里有些jar包不需要用到,用到的都在里面。附加上链接地址:
http://download.csdn.net/download/qq_25313187/10032339
相关文章推荐
- SSM简单整合(Mybatis,Spring,SpringMVC,基于注解和xml文件配置)
- 纯代码(基于注解和代码,零xml)整合SSHM(Spring、SpringMVC、Hibernate、Mybatis)
- springMVC+hibernate+jdbc+mybatis整合 支持全注解方式 支持全配置方式
- MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(2)
- spring,springmvc,mybatis基本整合(一)--xml文件配置方式(1)
- spring、springMVC、mybatis整合配置文件问题
- 整合mybatis和spring,配置spring注解
- SSM(spring-springmvc-mybatis)整合开发xml配置文件内容springmvc-config.xml,application.xml,web.xml
- Spring ,Springmvc,mybatis三大框架整合的配置文件支持
- 基于maven的SpringMVC,Spring,MyBatis的全注解配置
- Java Web开发之Spring | SpringMvc | Mybatis | Hibernate整合、配置、使用
- Spring详解之四:SpringMVC整合Mybatis相关配置文件信息
- 基于注解的Spring MVC整合Hibernate(所需jar包,spring和Hibernate整合配置,springMVC配置,重定向,批量删除)
- spring和mybatis的整合配置(包括springmvc)
- SSM基于注解配置Spring4.1.6、springMVC4.1.6、mybatis3.2.8的maven框架搭建
- javaWeb项目SpringMVC3.2.1与Mybatis3.0.4整合实例(Mybaits-spring配置文件的几种方式)之一SqlSessionDaoSupport方式
- spring、springmvc、mybatis整合文件配置详解
- spring整合mybatis,springMVC的0配置文件方式
- SpringMVC+MyBatis整合(3)特别篇SpringMVC配置