spring、springmvc、mybatis整合配置(注解)
来源:互联网 发布:qq飞车f22猛禽性能数据 编辑:程序博客网 时间:2024/05/19 05:02
最近整合ssm框架费了不少功夫,所以,把详细配置过程列出来,不足的地方望大侠们指正!
项目目录:
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 {@Autowiredprivate 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;@Servicepublic class UserServiceImpl implements IUserService{@Autowiredprivate IUserDao iUserDao;@Overridepublic 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
阅读全文
0 0
- spring、springmvc、mybatis整合配置(注解)
- spring&springmvc&mybatis整合注解
- SSM(spring+springMVC+mybatis)整合配置
- SSM简单整合(Mybatis,Spring,SpringMVC,基于注解和xml文件配置)
- SpringMVC+Spring+Mybatis整合配置
- Spring+SpringMVC+Mybatis配置整合
- [spring + springmvc + mybatis] 配置整合
- SpringMVC+Spring+Mybatis整合配置
- Spring+SpringMVC+MyBatis整合配置
- 整合mybatis和spring,配置spring注解
- SpringMVC+Mybatis整合注解
- SpringMVC+Mybatis整合注解
- Spring+Hibernate+SpringMVC整合配置(注解式)
- Spring SpringMVC Mybatis 全注解配置
- spring和mybatis的整合配置(包括springmvc)
- Spring+SpringMVC+MyBatis 整合(一)(Maven配置)
- SSM(Spring+SpringMVC+Mybatis)框架整合配置方法
- springmvc+spring+mybatis整合的配置
- 云数据库管理与数据迁移
- vue 1.x 交互-仿百度下拉列表
- 管理分区表
- 【LeetCode算法练习(C++)】Reverse Nodes in k-Group
- 总结JS 常用函数
- spring、springmvc、mybatis整合配置(注解)
- Java反射打印类的信息,包括类的成员函数、成员变量(只获取成员函数)
- (TODO)Empirical Analysis of Predictive Algorithm for Collaborative Filtering
- 通过3D-MAX制作三维地图模型
- HTTP协议详解
- VS2017 Xamarin扫描二维码并跳转网页
- 题目51:管闲事的小明
- 设计模式之模版方法
- 總結——關於2017 10 20測試的分析總結