SSM框架(spring+springmvc+mybatis)
简介
本次简单的介绍一下大致的ssm框架的构造。主要创建包(package和文件夹)说起。然后就是一些相关配置问题(springmvc+mybatis)。因为是教学,需要手动搭建比较费时间。
工具
IDEA,相关jar,还有一些前端的东东(美化工具,这里以bootstrap为例)
基本框架
首先要创建一个工程项目Project,名字随意。注意要添加Web
搭建好后可以先在web文件夹下创建一些文件夹还有WEB-INF(有些人喜欢全放在WEB-INF中,也可以,记住路径可以)
css:当然是前端美化工具css(bootstrap)
fonts:同上
images:图片储存地方
js:jquery和bootstrap相关储存地方
jsp:放jsp文件
lib:夹包,IDEA需要设置Excluded(否则会添加很久很久)
tld:翻页功能设置tld
然后就是src文件夹下的创建夹包简单介绍一下
common.utils下放得是翻页功能的类
core下的就是后台相关功能的夹包如
interceptor是拦截器配置
po是模板连接数据库的样式需要加get和set
service就是服务接口
dao就是数据库连接相关类和xml
最后就是web.controller控制台,就是前端访问后端的请求响应即springmvc相关
配置文件
首先是lib文件夹下(一共35个,可以看自己需要添加):
web.xml配置spring和springmvc及中文乱码问题
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 配置加载Spring文件的监听器-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- 编码过滤器 -->
<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>*.action</url-pattern>
</filter-mapping>
<!-- 配置Spring MVC前端核心控制器 -->
<servlet>
<servlet-name>crm</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc-config.xml</param-value>
</init-param>
<!-- 配置服务器启动后立即加载Spring MVC配置文件 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>crm</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!-- 系统默认页面 -->
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
springmvc-config.xml
<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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- 加载属性文件 -->
<context:property-placeholder
location="classpath:resource.properties" />
<!-- 配置扫描器 -->
<context:component-scan
base-package="com.team5408.core.web.controller" />
<!-- 注解驱动:配置处理器映射器和适配器 -->
<mvc:annotation-driven />
<!--配置静态资源的访问映射,此配置中的文件,将不被前端控制器拦截 -->
<mvc:resources location="/js/" mapping="/js/**" />
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/fonts/" mapping="/fonts/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<!-- 配置视图解释器ViewResolver -->
<bean id="jspViewResolver" class=
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.team5408.core.interceptor.LoginInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
</beans>
applicationContext.xml
<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-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--读取db.properties -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置数据源 -->
<bean id="dataSource"
class="org.apache.commons.dbcp2.BasicDataSource">
<!--数据库驱动 -->
<property name="driverClassName" value="${jdbc.driver}" />
<!--连接数据库的url -->
<property name="url" value="${jdbc.url}" />
<!--连接数据库的用户名 -->
<property name="username" value="${jdbc.username}" />
<!--连接数据库的密码 -->
<property name="password" value="${jdbc.password}" />
<!--最大连接数 -->
<property name="maxTotal" value="${jdbc.maxTotal}" />
<!--最大空闲连接 -->
<property name="maxIdle" value="${jdbc.maxIdle}" />
<!--初始化连接数 -->
<property name="initialSize" value="${jdbc.initialSize}" />
</bean>
<!-- 事务管理器 -->
<bean id="transactionManager" class=
"org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 传播行为 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="select*" propagation="SUPPORTS"
read-only="true" />
<tx:method name="get*" propagation="SUPPORTS"
read-only="true" />
</tx:attributes>
</tx:advice>
<!-- 切面 -->
<aop:config>
<aop:advisor advice-ref="txAdvice"
pointcut="execution(* com.team5408.core.service.*.*(..))" />
</aop:config>
<!-- 配置 MyBatis的工厂 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置MyBatis的核心配置文件所在位置 -->
<property name="configLocation"
value="classpath:mybatis-config.xml" />
</bean>
<!-- 接口开发,扫描 com.team5408.core.dao包 ,写在此包下的接口即可被扫描到 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.team5408.core.dao" />
</bean>
<!-- 配置扫描@Service注解 -->
<context:component-scan base-package="com.team5408.core.service"/>
</beans>
db.properties(配置数据库连接信息)
还有一些配置文件这里就不展示了
总结:这里只是处设置好配置文件,注意自己mysql版本设置,还有一些夹包的版本遗留问题,到目前为止就没什么问题。
登录功能的实现
jsp和po就不介绍,前面几章就介绍过了。
主要看代码。首先是controller包下主要代码
@Controller
public class AdminController {
@Autowired
private AdminService adminService;
/**
*登录验证
* 获取admincode,password,进行判断
* 没有通过判定创建model,返回主页面
*/
@RequestMapping(value="/alogin.action",method = RequestMethod.POST)
public String alogin(String admincode, String password, Model model, HttpSession session){
Admin admin=adminService.findAdmin(admincode, password);
if(admin!=null){
session.setAttribute("AMDIN_SESSION",admin);
return "redirect:admin/list.action";
}else {
model.addAttribute("msg","管理账号或密码错误,请重新输入");
return "login";
}
}
}
interceptor拦截器下代码
package com.team5408.core.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import com.team5408.core.po.Admin;
/**
* 登录拦截器
*/
public class LoginInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler)
throws Exception {
// 获取请求的URL
String url = request.getRequestURI();
// URL:除了登录请求外,其他的URL都进行拦截控制
if (url.indexOf("/alogin.action") >= 0) {
return true;
}
// 获取Session
HttpSession session = request.getSession();
Admin user = (Admin) session.getAttribute("AMDIN_SESSION");
// 判断Session中是否有用户数据,如果有,则返回true,继续向下执行
if (user != null) {
return true;
}
// 不符合条件的给出提示信息,并转发到登录页面
request.setAttribute("msg", "您还没有登录,请先登录!");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp")
.forward(request, response);
return false;
}
@Override
public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception {
}
}
service包下接口及service.impl包下代码
public interface AdminService {
//通过账号和密码查询管理员
public Admin findAdmin(String admincode,String password);
}
@Service("AdminService")
@Transactional
public class AdminServiceImpl implements AdminService {
//注入AdminDao
@Autowired
private AdminDao adminDao;
//通过账号和密码查询用户
@Override
public Admin findAdmin(String admincode,String password){
Admin admin=this.adminDao.findAdmin(admincode,password);
return admin;
}
}
dao包下接口和xml配置
public interface AdminDao {
/**
*通过账号和密码查询管理员
*/
public Admin findAdmin(@Param("admincode") String admincode,
@Param("password") String password);
}
<?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.team5408.core.dao.AdminDao" >
<!-- 查询用户 -->
<select id="findAdmin" parameterType="String" resultType="admin">
select * from sys_admin
where admin_code = #{admincode}
and admin_password =#{password}
and admin_state = '1'
</select>
</mapper>
到这里就可以完成基本的登录功能。