SSM框架搭建

Posted z_fishLong

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SSM框架搭建相关的知识,希望对你有一定的参考价值。

常用框架

Springmvc+spring+hibernate

Springmvc+spring+mybatis

Spring+Struts2+hibernate


开发步骤:

1) 画草图

 

 

2) 创建表


3) 创建web工程


4) 导入jarmybatis包,spring(springmvc),数据库驱动

aopalliance-1.0.jar
asm-3.3.1.jar
cglib-2.2.2.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
ehcache-core-2.6.5.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
junit-4.9.jar
log4j-1.2.16.jar
mybatis-3.2.2.jar
mybatis-ehcache-1.0.2.jar
mybatis-spring-1.2.0.jar
mysql-connector-java-5.1.10-bin.jar
ognl-3.0.6.jar
slf4j-api-1.6.2.jar
slf4j-log4j12-1.6.2.jar
spring-aop-3.2.2.jar
spring-beans-3.2.2.jar
spring-context-3.2.2.jar
spring-core-3.2.2.jar
spring-expression-3.2.2.jar
spring-jdbc-3.2.2.jar
spring-orm-3.2.2.jar
spring-tx-3.2.2.jar
spring-web-3.2.2.jar
spring-webmvc-3.2.2.jar

5) 创建PO实体对象

import java.io.Serializable;
import java.util.List;
public class Person implements Serializable 
	private Integer id;
	private String name;
	private Integer age;
	private String remark;
	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;
	
	public String getRemark() 
		return remark;
	
	public void setRemark(String remark) 
		this.remark = remark;
	


 

6) 创建mapper映射文件

<?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.ilmare.ssm.mapper.PersonMapper">
	<resultMap type="com.ilmare.ssm.domain.Person" id="personRM">
		<id property="id" column="ID"/>
		<result property="name" column="USER_NAME"/>
		<result property="age" column="AGE"/>
		<result property="remark" column="REMARK"/>
	</resultMap>
	
	<!-- 带条件查询 -->
	<select id="find" parameterType="map" resultMap="personRM">
		select * from person
		where 1=1
		<if test="name!=null"> and user_name like #name</if>
		<if test="ageStart!=null"> and age>#ageStart</if>
		<if test="ageEnd!=null"><![CDATA[ and age<=#ageEnd]]></if>
	</select>
</mapper>

7) 创建Mapper接口

import java.util.List;
import java.util.Map;
public interface PersonMapper 
	public List<Person> find(Map map);

8) 创建PersonDao接口

import java.util.List;
import java.util.Map;
public interface PersonDao 
	public List<Person> find(Map map);

9) 创建PersonDaoImpl

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;
import com.ilmare.ssm.bean.Person;
import com.ilmare.ssm.dao.PersonDao;
import com.ilmare.ssm.mapper.PersonMapper;

@Repository("personDao")
public class PersonDaoImpl extends SqlSessionDaoSupport  implements PersonDao
	//这两个随便注入一个
	//sqlSessionTemplate
	//sqlSessionFactory
	
	/*
	 * mybatis3.0.0+mybatis-psring1.0.0无需,整合包自己注入
	 * mybatis3.2.2+mybatis-spring1.2.0 必须自己注入sqlSessionFactory;
	 */
        @Resource
	public void setSqlSessiionFactory(SqlSessionFactory sqlSessionFactory) 
		super.setSqlSessionFactory(sqlSessionFactory);
	
	
	@Override
	public List<Person> find(Map map) 
		PersonMapper personMapper=this.getSqlSession().getMapper(PersonMapper.class);
		return personMapper.find(map);
	

	

10) 创建PersonService接口

import java.util.List;
import java.util.Map;
import com.ilmare.ssm.domain.Person;
public interface PersonService 
	public List<Person> find(Map map);

11) 创建PersonServiceImpl

@Service
public class PersonServiceImpl implements PersonService 
	@Resource
	PersonDao personDao;
	
	public List<Person> find(Map map)
		return personDao.find(map);
	

12) 创建PersonController

@Controller
public class PersonController 
	@Resource
	PersonService personService;
	
	//查询,将查询结果传递到页面
	@RequestMapping("/person/list.action")	//Controller加载时会自动创建访问路径 /person/list;/person/list.action
	public String list(Model model)
		List<Person> personList = personService.find(null);
		model.addAttribute("personList", personList);		//传递到页面
		
		return "/person/jPersonList.jsp";
	

13) 创建列表页面jPersonList.jsp

14) 配置sqlMapConfig.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>

</configuration>

15) 配置application-context.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: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-3.0.xsd
				         http://www.springframework.org/schema/context
				         http://www.springframework.org/schema/context/spring-context-3.0.xsd
				         http://www.springframework.org/schema/tx
				         http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
				         http://www.springframework.org/schema/aop 
				         http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
	
	<!-- 1.包扫描,service dao -->
	<context:component-scan base-package="com.ilmare.ssm.dao,com.ilmare.ssm.service"/>
	
	<!-- 2.jdbc.properties属性配置文件 -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 3.数据源 DataSource -->
	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="$jdbc.driverclass"/>
		<property name="url" value="$jdbc.url"/>
		<property name="username" value="$jdbc.username"/>
		<property name="password" value="$jdbc.password"/>
	</bean>
	
	<!-- 4.SqlSessionFactory -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"/>
		<!-- spring和mybatis整合 -->
		<!-- 1)引入mybatis核心配置文件 -->
		<property name="configLocation" value="classpath:sqlMapCongfig.xml"/>
		
		<!-- 2)扫描指定目录下的所有Mapper.xml -->
		<property name="mapperLocations" value="classpath:com/ilmare/ssm/mapper/*.xml"/>
	</bean>
	
	<!-- 5.事务 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	 </bean>

        <tx:advice id="txAdvice" transaction-manager="txManager">
 		<tx:attributes>
 			<tx:method name="save*" propagation="REQUIRED"/>
 			<tx:method name="insert*" propagation="REQUIRED"/>
 			<tx:method name="update*" propagation="REQUIRED"/>
 			<tx:method name="delete*" propagation="REQUIRED"/>
			
 			<tx:method name="find*" read-only="true"/>
 			<tx:method name="get*" read-only="true"/>
 			<tx:method name="view*" read-only="true"/>
			
			 <tx:method name="*" read-only="true"/>
		 </tx:attributes>
	 </tx:advice>

        <aop:config>
		<aop:pointcut expression="execution(* com.ilmare.ssm.service.*.*(..))" id="pointCut"/>
	        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointCut"/>
	 </aop:config>
	
			         
</beans>

jdbc.properties

jdbc.driverclass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatisdb?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=263938

16) 配置springmvc-servlet.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:mvc="http://www.springframework.org/schema/mvc"
	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-3.0.xsd 
	http://www.springframework.org/schema/mvc 
	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
	http://www.springframework.org/schema/context 
	http://www.springframework.org/schema/context/spring-context-3.0.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">
	
	<!-- 1.包扫描controller -->
	<context:component-scan base-package="com.ilmare.ssm.web.controller"/>
	
	<!-- 2.内部资源视图解析器  -->
  	<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/pages"/>
		<property name="suffix" value=""/>
	 </bean>
	
</beans>

17) 配置web.mxl

<?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" 
		xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
		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">
	 	
	<!-- 1.spring容器 listener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application-context.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 2.springmvc servlet -->
	<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:springmvc-servlet.xml</param-value>
		</init-param>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>springmvc</servlet-name>
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	 
	<!-- 3.spring 中文乱码 filter -->
	<!-- 编码过滤器,解决中文乱码 -->
	<filter>
		<filter-name>SpringEncoding</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>
	</filter>
	<filter-mapping>
		<filter-name>SpringEncoding</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>	
</web-app>

18) 发布,测试



以上是关于SSM框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

ssm框架的搭建

使用IDEA搭建SSM框架

idea ssm框架搭建

使用maven搭建ssm框架的javaweb项目

SSM框架整合 环境搭建

快速搭建ssm框架