SSM框架整合配置案例
程序员文章站
2024-01-15 22:13:06
...
一、SSM
1.SpringMVC
它用于web层,相当于controller(等价于传统的servlet和struts的action),用来处理用户请求。
2.Spring
IOC容器,它可以装载bean,创建对象
3.Mybatis
操作数据库
二、配置案例
1.导入jar包
2.配置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">
<!-- 指定spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>
<!-- 监听器 初始化spring的配置文件 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置servlet请求分发器,用来处理请求,找到对应的controller处理请求 -->
<servlet>
<!-- 默认springmvc配置文件在WEB-INF下,<servlet-name>+servlet.xml -->
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 配置contextConfigLocation,指定springmvc配置文件位置 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<!-- 处理请求编码,默认只对post有效 -->
<filter>
<filter-name>encoding</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>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
3.spring配置文件
src下beans.xml(web.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
<!-- 导入数据库配置信息 -->
<context:property-placeholder location="classpath:JDBC.properties" />
<!-- 扫描service,dao层 -->
<context:component-scan base-package="cn.wang.*"></context:component-scan>
<!-- dataSource -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- sessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- spring-mybatis整合注入mapper映射文件 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 指定要扫描的包 -->
<property name="basePackage" value="cn.wang.dao"></property>
<!-- sqlSessionFactory ,使用sqlSessionFactoryBeanName属性注入 -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 事务 -->
<!-- 声明注解事务 -->
<tx:annotation-driven transaction-manager="transcationManager" />
<!-- 事务管理器 -->
<bean id="transcationManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- dataSource -->
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
4.springmvc配置文件
src下springmvc.xml(web.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"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.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-3.1.xsd">
<!-- spring mvc注解支持 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- spring创建bean 基包扫描 -->
<context:component-scan base-package="cn.wang.controller"></context:component-scan>
<!-- 创建 一个controller bean对象 -->
<!--
<bean name="/login.do" class="cn.wang.controller.UserController"></bean>
-->
<!-- 内部资源视图解析器 -->
<!-- <bean id="irc" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
前缀
<property name="prefix" value="/WEB-INF/"></property>
后缀
<property name="suffix" value=".jsp"></property>
</bean> -->
</beans>
5.数据库配置信息
src下JDBC.properties(beans.xml中导入)
6.实体类User
7.dao层
(1)接口IUserDao
package cn.wang.dao;
import java.io.Serializable;
import java.util.List;
import org.springframework.stereotype.Repository;
import cn.wang.domain.User;
/**
* User Dao层
* @author wangchao
* 接口下没有实现类,有Mapper.xml
* 当前对象就是dao层对象
*/
@Repository
public interface IUserDao {
List<User> findAll();
User findByUsername(String username);
User findById(Serializable id);
void add(User user);
void deleteById(Serializable id);
void updata(User user);
}
(2)IUserDao.xml(对应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="cn.wang.dao.IUserDao">
<select id="findByUsername" resultType="cn.wang.domain.User">
select * from tbl_user where uname=#{username}
</select>
//... ...
</mapper>
8.service层
(1)接口IUserService
import java.io.Serializable;
import java.util.List;
import cn.wang.domain.User;
public interface IUserService {
List<User> findAll();
User findByUsername(String username);
User findById(Serializable id);
void add(User user);
void deleteById(Serializable id);
void updata(User user);
}
(2)接口实现类UserServiceImpl
9.Controller层
@Controller
//父路径
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login1(User user, Model model) {
System.out.println(user);
User login = userService.findByUsername(user.getUname());
if (login != null && user.getUpsw().equals(login.getUpsw())) {
model.addAttribute("user", login);
return "/WEB-INF/welcome.jsp";
} else {
// 错误信息
model.addAttribute("errMsg", "用户名密码错");
return "/index.jsp";
}
}
}
10.前台页面
(1)/index.jsp
(2)/WEB-INF/weclome.jsp