SSM(Maven集成)
Posted Beyondづ
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM(Maven集成)相关的知识,希望对你有一定的参考价值。
ssm全称:Spring+SpringMVC+Mybatis
ssm简介:
1、Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
2、SpringMVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
3、MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
创建步骤如下:
一、使用IntelliJ IDEA 创建Maven管理的JAVA Web项目
①新建项目,选择Maven Module
在本地磁盘用户的。.m2目录下生成settings文件:
Maven工程搭建完毕!!!
idea严格目录区分:main下必须书写java和resources,test也如此。
② 开发步骤:
书写pom.xnl下载所需jar
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.hq</groupId> <artifactId>SpringMVC_SSM</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>SpringMVC_SSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!--junit配置--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.17.1-GA</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.3.release</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency> </dependencies> <build> <finalName>SpringMVC_SSM</finalName> </build> </project>
3.由于Maven对oracle数据库的不支持,我们需要将Oracle装载到本地仓库:
(1).查询Oracle安装目录:D:\\app\\asus\\product\\11.2.0\\dbhome_1\\jdbc\\lib
配置Maven3.3.9 bin到环境变量
然后可以通过cmd命令行 mvn -version测试有没有测试成功!!!
(2).定位到Oracle安装目录的jdbc文件夹
4.搭建分层架构---实现添加学生
实体类UserInfo:
package cn.hq.entity; /** * Created by hq on 2017/1/16. */ public class UserInfo { private Integer id; private String name; private Integer age; 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 getAge() { return age; } public void setAge(Integer age) { this.age = age; } } dao接口: 无需dao实现类,通过代理生成 public interface IUserInfoDao { public void add(UserInfo info); } service层: public interface IUserInfoService { public void add(UserInfo info); } service实现类: public class UserInfoServiceImpl implements IUserInfoService { private IUserInfoDao dao; public void add(UserInfo info) { dao.add(info); } public IUserInfoDao getDao() { return dao; } public void setDao(IUserInfoDao dao) { this.dao = dao; } }
控制器类:controller
public class UserInfoController implements Controller { private IUserInfoService service; public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { String uname=httpServletRequest.getParameter("uname"); Integer uage=Integer.valueOf(httpServletRequest.getParameter("uage")); UserInfo info=new UserInfo(); info.setName(uname); info.setAge(uage); service.add(info); return new ModelAndView("/welcome.jsp"); } public IUserInfoService getService() { return service; } public void setService(IUserInfoService service) { this.service = service; } }
由于是整合MyBatis,需要书写核心配置文件:
小配置:UserInfoDao.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="cn.hq.dao.IUserInfoDao"> <select id="add" parameterType="UserInfo"> insert into userinfo values(SEQ_Num.nextval,#{name},#{age}) </select> </mapper>
MyBatis配置文件:mybatis-config.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> <typeAliases> <package name="cn.hq.entity"/> </typeAliases> <mappers> <package name="cn.hq.dao"/> </mappers> </configuration>
applicationContext.xml:进行植入dao、service、controller、事务管理器、数据源、sessionFactory
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd " > <!-- 01.配置数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 1.1 关联jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 02.配置SessionFactory --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <!--<property name="mapperLocations" value="classpath:cn/hq/dao/IUserInfoDAO.xml"></property--> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 03.生成dao代理對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> <property name="basePackage" value="cn.hq.dao"></property> </bean> <!--04.配置service--> <bean id="userService" class="cn.hq.service.UserInfoServiceImpl"> <property name="dao" ref="IUserInfoDAO"></property> </bean> <!-- 05.配置action --> <bean id="/userAction.do" class="cn.hq.controller.UserInfoController"> <property name="service" ref="userService"></property> </bean> <!-- 06.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 07.配置开启事务操作 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--指定在连接方法上应用的事务属性 --> <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- aop配置 --> <aop:config> <aop:pointcut expression="execution(* *..service.*.*(..))" id="stockPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="stockPointcut"/> </aop:config> </beans>
index.jsp页面:
<body> <form action="${pageContext.request.contextPath }/userAction.do" method="post"> 用户名:<input name="uname"/> <br/><br/> 年龄:<input name="uage"/><br/><br/> <input type="submit" value="add"/> </form> </body>
Web.xml:进行配置中央调度器
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 1.针对Spring配置:读取配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 注册ServletContext监听器,创建容器对象,并且将ApplicationContext对象放到Application域中 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 针对SpringMVC的配置::::::中央调度器:本质上一个serlvet 配置的关于SpringmVC组件 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- 解决乱码的过滤器 --> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
实现效果:
注:若出现此类异常--org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
解决方案:参见博客
http://www.cnblogs.com/jstarseven/p/5803697.html
生活不止眼前的苟且,还有诗和远方。
以上是关于SSM(Maven集成)的主要内容,如果未能解决你的问题,请参考以下文章
SSM+Redis+Shiro+Maven框架搭建及集成应用
java 整合redis缓存 SSM 后台框架 rest接口 shiro druid maven b