ssm框架搭建(Spring+SpringMVC+MyBatis)

一:前言

学过ssm框架,但是一直没有自己搭建过,所以今天就想搭建一个属于自己的ssm框架,方便以后复习,写的不好,勿喷。

后续会出一个maven版本的项目,现在只是简单的项目搭建

二:实现

1.新建一个Dynamic Web Project项目为ssmDemo

2.第一步:在/ssmDemo/WebContent/WEB-INF/lib导入jar包,jar包就不提供了,有需要的可以私信我

3.第二步:整合spring和mybatis

1.配置mybatis自身的xm文件mybatisConfig.xml,数据库放在spring里配置

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

2. 配置整合spring与mybatis的spring-mybatis.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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	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.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
	
	<!--配置自动扫描包  -->
	<context:component-scan base-package="cn.com.dao" ></context:component-scan>
	<!--导入配置文件  -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!--配置数据源:这里用的是阿里的jar包  -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
		<property name="username" value="${jdbc.username}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="driverClassName" value="${jdbc.className}"></property>
	</bean>
	
	
	<!-- 整合spring与mybatis -->
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--指向配置的数据源  -->
		<property name="dataSource" ref="dataSource"></property>
		<!-- 读取mybatisConfig.xml配置 -->
		<property name="configLocation" value="classpath:resources/mybatis/mybatisConfig.xml"></property>
		<!--读取mybatis映射文件配置  -->
		<property name="mapperLocations" value="classpath:resources/mapper/*.xml"></property>
	</bean>
	
	<!--配置mybatis  -->
	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
		<constructor-arg ref="sqlSessionFactoryBean"></constructor-arg>
	</bean>
	
	<!--1.配置事务管理器-->
		<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
			<property name="dataSource" ref="dataSource"></property>
		</bean>
		
		<!--2. 配置事务的属性 -->
		<tx:advice id="txAdvice" transaction-manager="transactionManager">
			<tx:attributes>
				<!-- 根据方法名指定方法的属性 -->
				<tx:method name="*" propagation="REQUIRED"/>
			</tx:attributes>
		</tx:advice>
	
		<!--配置事务切入点,以及把事务切入点和事务属性关联起来  -->
		<aop:config>
				<aop:pointcut expression="execution (* cn.com.service.impl.*.*(..))" id="pointcut"/>
				<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
		</aop:config>
</beans>
jdbc.username=root
jdbc.password=123456
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.className=com.mysql.jdbc.Driver

3.配置整合spring的spring-main.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: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.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

		<!--配置自动扫描service下的基类  -->
		<context:component-scan base-package="cn.com.service"></context:component-scan>
		
		
</beans>

4.

<?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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		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.2.xsd">

	<!-- 扫描controller下的基类 -->
	<context:component-scan base-package="cn.com.controller"></context:component-scan>

	<!-- 配置mvc注解扫描 -->
	<mvc:annotation-driven></mvc:annotation-driven>
	
	<!-- 配置视图解析器 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
			<property name="prefix" value="/WEB-INF/views/"></property>
			<property name="suffix" value=".jsp"></property>
	</bean>
	
</beans>

5.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>ssmDemo</display-name>
  
  <!-- 加载spring容器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:resources/spring/spring-*.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!--配置dispatchServlet  -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:resources/spring/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 解决post乱码 -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

三:写一个简单功能测试一下

1.测试页面

<a href="selectAll"> pick up</a>

2.创建一个实体类,并在数据库创建一个对应的表

CREATE TABLE `person` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(11) NOT NULL,
  `age` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
package cn.com.enitys;

public class Person {
	private Integer id;
	
	private String name;
	
	private Integer age;

	public Integer getId() {
		return id;
	}

	public Person() {
		super();
	}

	public Person(Integer id, String name, Integer age) {
		super();
		this.id = id;
		this.name = name;
		this.age = age;
	}

	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;
	}

}

3.写一个controller类

package cn.com.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.com.enitys.Person;
import cn.com.service.impl.PersonService;

@Controller
public class PersonController {
	
	@Resource(name="personService")
	private PersonService personService;
	
	@RequestMapping("selectAll")
	public String selectAll(Map map) throws Exception {
		List<Person> lists=personService.selectAll();
		System.out.println(lists);
		map.put("list", lists);
		return "list";
	}

}

4.创建一个service类

package cn.com.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.com.dao.DaoSupport;
import cn.com.enitys.Person;

@Service("personService")
public class PersonService {
	
	@Resource(name="daoSupport")
	private DaoSupport<Person> dao;
	
	public List<Person> selectAll() throws Exception{
		return dao.findForList("cn.com.enitys.Person.queryPerosn", null);
		
	}

}

5.创建mybatis的映射文件

<?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">
 <!--namespace:该映射文件的唯一标识 -->
<mapper namespace="cn.com.enitys.Person">
	
	<select id="queryPerosnById" resultType="cn.com.enitys.Person" parameterType="int">
		select * from person where id = #{id}
	</select>
	
	<select id="queryPerosn" resultType="cn.com.enitys.Person" parameterType="int">
		select * from person
	</select>
	
	<insert id="addPerson" parameterType="cn.com.enitys.Person">
		insert into person(id,name,age) values(#{id},#{name},#{age})
	
	</insert>
	
	<update id="updatePerson">
		update person set 
		name=#{name},
		age=#{age}
		where id = #{id}
	</update>
	
	<delete id="deletePerson">
		delete from person
		where id = #{id}
	
	</delete>
	
</mapper>

6.结果页面

发布了64 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_39093474/article/details/104040292