搭建SSM框架

好长时间没有碰SSM框架,怕自己忘干净了,整理一下!

ssm的jar包在这里:链接:https://pan.baidu.com/s/1suDK4fW2myvVBVqfteZImw 密码:aln5

首先配置下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_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SSM</display-name>
 
  <!-- 配置servlet监听器 -->
  <listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
  <!-- 指定spring配置文件所在的位置 -->
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:config/applicationContext.xml</param-value>
  </context-param>

  <!-- 注册字符集过滤器 -->
  <filter>
  	<filter-name>characterEncoding</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>characterEncoding</filter-name>
 	<url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 配置中央调度器 -->
<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:config/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>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  
  </welcome-file-list>
  
  
</web-app>

然后就可以开心的写代码了

首先定义一个实体类:student

package com.ayit.bean;

public class Student {
	
	private String name;
	private int age;
	
	
	public Student() {
		super();
		// TODO Auto-generated constructor stub
	}
	
	
	
	public Student(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}



	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}



	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
	
}

 

紧接着,定义他的控制器,service,和Dao;

controller;

package com.ayit.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.ayit.Service.IStudentService;
import com.ayit.bean.Student;

public class StduentController implements Controller {

	private IStudentService studentService;
	
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}


	@Override
	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		int age2 = Integer.valueOf(age);
		System.out.println("name = " + name);
		System.out.println("age = " + age2);
		Student student = new Student(name,age2);
		studentService.addStudent(student);
		
		return new ModelAndView("/show.jsp");
	}

}
package com.ayit.Service;

import com.ayit.Dao.IStudentDao;
import com.ayit.bean.Student;

public class StudentServiceImpl implements IStudentService {
	private IStudentDao studentDao;
	
	
	public void setStudentDao(IStudentDao studentDao) {
		this.studentDao = studentDao;
	}


	@Override
	public void addStudent(Student student) {
		studentDao.addStudent(student);
		
	}

}

 

package com.ayit.Dao;

import com.ayit.bean.Student;

public interface IStudentDao {
	public void addStudent(Student student);
}

 

以及student.xml和myBatis.xml

Student.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="com.ayit.Dao.IStudentDao">
	<insert id="addStudent" >
		insert into Student(name,age) values(#{name},#{age})
	</insert>
</mapper>

myBatis.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="com.ayit.bean"/>
	</typeAliases>
	
	<mappers>
		<package name="com.ayit.Dao"/>
	</mappers>
</configuration>

 

接下来就是配置applicationContext.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:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
	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.xsd
					http://cxf.apache.org/bindings/soap 
					http://cxf.apache.org/schemas/configuration/soap.xsd
					http://cxf.apache.org/jaxws 
					http://cxf.apache.org/schemas/jaxws.xsd
					http://www.springframework.org/schema/context
						http://www.springframework.org/schema/context/spring-context.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop.xsd
						http://www.springframework.org/schema/tx 
						http://www.springframework.org/schema/tx/spring-tx.xsd">
						
	

	<!-- 注冊c3p0数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	
	<!-- 加载jdbc.properties文件 -->
	<context:property-placeholder location="classpath:config/jdbc.properties"/>

	<!--  注册sqlsessionFactoryBean-->
	
	<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref ="dataSource"/>
		<!-- 指定mybatis的主配置文件路径 -->
		<property name="configLocation" value="classpath:config/myBatis.xml"/>
		<property name="mapperLocations" value="classpath:com/ayit/Dao/IStudent.xml"></property>
	</bean>
	
	<!-- 注册mapper的扫描注册器 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryBean"/>
		<property name="basePackage" value="com.ayit.Dao" />
	</bean>
	
	<!-- 注册service服务 -->
	<bean id="studentService" class="com.ayit.Service.StudentServiceImpl">
	 	<property name="studentDao" ref="IStudentDao"/>
	</bean>
	
	 <!--配置事务管理器  -->
	 
	<bean 	id="transactionManager" 
			class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	
	<bean id="/test/register.do" class="com.ayit.controller.StduentController">
		<property name="studentService" ref="studentService"></property>
	</bean>	
	
	<!-- 注册事务通知 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/>
		</tx:attributes> 
	</tx:advice>
	
	<aop:config>
		<aop:pointcut expression="execution(* *..Service.*.*(..))" id="myPointcut"/>
		<aop:advisor advice-ref="transactionAdvice" pointcut-ref="myPointcut"/>
	</aop:config>
	
	
	
</beans>

以上就是搭建ssm的所有配置,项目结构如下

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值