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.結果頁面

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章