手把手教你ssm框架搭建
程序员文章站
2024-01-19 08:02:16
...
ssm框架搭建大致步骤是:
1.先配置前端控制器(DispatcherServlet)
前端控制器其实就是一个servlet 所以应该配置在web.xml中 和配置普通的servlet差不多 (它会自动去web-inf下去查找一个名字为[servlet_name]-servlet.xml的文件 但是我们一般没有按这个规范命名或者没有将该文件放在web-info下面 所以此时我们应该在<init-param>标签下指明该文件的位置)
2.按理说 第二步 应该配置处理器映射器 和 处理器适配器 但是系统都会给我们默认的配置 如果没有特殊要求 按默认的配置就可以
3.配置组件扫描器(component-scanner)和注解驱动(annotation-driven)
4.配置视图解析器(将逻辑视图转换为物理视图)
5.配置编码过滤器 其实就是一个Filter
6.开启spring的IOC监听
7.读取spring.xml(用context-param标签 其实spring和springMVC用一个标签就可以)
8.配置数据源
9.配置事务管理器
10.配置事务拦截器
Student.java
StudentMapper.java
StudentMapper.xml
IStudentService.java
StudentServiceImpl.java
以下是配置文件 都有注释
web.xml
1.先配置前端控制器(DispatcherServlet)
前端控制器其实就是一个servlet 所以应该配置在web.xml中 和配置普通的servlet差不多 (它会自动去web-inf下去查找一个名字为[servlet_name]-servlet.xml的文件 但是我们一般没有按这个规范命名或者没有将该文件放在web-info下面 所以此时我们应该在<init-param>标签下指明该文件的位置)
2.按理说 第二步 应该配置处理器映射器 和 处理器适配器 但是系统都会给我们默认的配置 如果没有特殊要求 按默认的配置就可以
3.配置组件扫描器(component-scanner)和注解驱动(annotation-driven)
4.配置视图解析器(将逻辑视图转换为物理视图)
5.配置编码过滤器 其实就是一个Filter
6.开启spring的IOC监听
7.读取spring.xml(用context-param标签 其实spring和springMVC用一个标签就可以)
8.配置数据源
9.配置事务管理器
10.配置事务拦截器
11.配置自动代理
***************************************************详细步骤**************************************************************
首先需要搭建一个动态web项目 大致步骤如下
项目结构如下
****************************************************项目代码************************************************************
register.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>注册页面</title>
</head>
<body>
<form action="regist.do" method="get">
<table align="left" >
<tr>
<td>name</td>
<td><input name="name" type="text"></td>
</tr>
<tr>
<td>age</td>
<td><input name="age" type="text"></td>
</tr>
<tr>
<td>
<input type="submit" value="submit">
</td>
</tr>
</table>
</form>
</body>
</html>
Student.java
package cn.liujd.bean;
import java.io.Serializable;
/**
* 普通的学生bean类
* @author liujd
*
*/
public class Student implements Serializable{
private static final long serialVersionUID = 6724577147867445725L;
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 == null ? null : name.trim();
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
StudentMapper.java
package cn.liujd.dao;
import cn.liujd.bean.Student;
/**
* dao层接口 与数据库交互
* @author liujd
*
*/
public interface StudentMapper {
//保存学生信息的方法
int saveStudent(Student student);
}
StudentMapper.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文件 -->
<mapper namespace="cn.liujd.dao.StudentMapper" >
<resultMap id="BaseResultMap" type="cn.liujd.bean.Student" >
<id column="id" property="id" jdbcType="INTEGER" />
<result column="name" property="name" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
</resultMap>
<sql id="Base_Column_List" >
id, name, age
</sql>
<insert id="saveStudent" parameterType="cn.liujd.bean.Student" useGeneratedKeys="true" keyProperty="id" >
insert into student
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="name != null" >
name,
</if>
<if test="age != null" >
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides="," >
<if test="name != null" >
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null" >
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
</mapper>
IStudentService.java
package cn.liujd.service;
import cn.liujd.bean.Student;
/**
* service层接口
* @author liujd
*
*/
public interface IStudentService {
//保存学生对象的方法
int savaStudent(Student student);
}
StudentServiceImpl.java
package cn.liujd.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.liujd.bean.Student;
import cn.liujd.dao.StudentMapper;
/**
* service层实现类
* @author liujd
*
*/
@Service
public class StudentServiceImpl implements IStudentService{
@Autowired
private StudentMapper mapper;
@Override
public int savaStudent(Student student) {
return mapper.saveStudent(student);
}
}
StudentController.java
package cn.liujd.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.liujd.bean.Student;
import cn.liujd.service.IStudentService;
/**
* web层
* @author liujd
*
*/
@Controller
public class StudentController {
@Autowired
private IStudentService service;
//存到数据库
@RequestMapping("regist.do")
public String regist(Student student) {
if(student != null) {
service.savaStudent(student);
}
return "register";
}
//用来跳转到注册页面
@RequestMapping("/register")
public String init(){
return "register";
}
}
以下是配置文件 都有注释
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
use=xxx
password=xxx
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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssmCombine</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<!-- 配置前端控制器 -->
<servlet>
<servlet-name>ssm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<!-- 读取springMVC配置文件 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
<!-- 加载时机 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm</servlet-name>
<url-pattern>/</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>
<!-- 默认是false 需要设置为true才能设置编码 -->
<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>
<!-- 开启IOC监听 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 初始化spring配置文件所在位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
</web-app>
spring.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.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 组件扫描 注解驱动 -->
<context:component-scan base-package="cn.liujd"></context:component-scan>
<mvc:annotation-driven/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!-- 读取数据源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>${driver}</value>
</property>
<property name="url">
<value>${url}</value>
</property>
<property name="username">
<value>${use}</value>
</property>
<property name="password">
<value>${password}</value>
</property>
<!-- 最大连接数 -->
<property name="maxActive">
<value>80</value>
</property>
<!-- 最大空闲连接数 -->
<property name="maxIdle">
<value>20</value>
</property>
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间 单位:毫秒 -->
<!-- 超过时间则抛出异常,如果设置为-1表示无限等待 -->
<property name="maxWait">
<value>3000</value>
</property>
</bean>
<!-- 创建sqlsessionFactory 生产sqlsession -->
<bean name="sqlSessionFactory" class=" org.mybatis.spring.SqlSessionFactoryBean ">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置mybatis接口代理开发 -->
<bean class=" org.mybatis.spring.mapper.MapperScannerConfigurer ">
<property name="basePackage" value="cn.liujd.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager" class=" org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置拦截service -->
<aop:config>
<aop:advisor advice-ref="txAdvice" pointcut="execution(* cn.liujd.service.*.*(..))"/>
</aop:config>
</beans>
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>
<settings>
<setting name="jdbcTypeForNull"
value="VARCHAR"></setting>
</settings>
<typeAliases>
<package name="cn.liujd.bean" />
</typeAliases>
<mappers>
<package name="cn.liujd.dao"/>
</mappers>
</configuration>
运行结果:
该项目的源代码以打包,下载地址为http://download.csdn.net/detail/liujiding/9901555
上一篇: Java JDK自带的观察者设计模式
下一篇: L3-010 是否完全二叉搜索树