SSM框架实例(登陆功能)

SSM框架实例(用户登录功能)*需考虑tomcat与jdk版本兼容性问题

第一步:新建一个web工程 First,导入搭建SSM需要的jar包到WebContent\WEB-INF\lib。

第二步:创建需要的类包controller、mapper、pojo、service、serviceImpl;

1.controller包中放控制层java类:

Login.java

@Controller//注解

public class Login {

//@Autowired可以对成员变量、方法和构造函数进行标注,来完成自动装配的工作

@Autowired//标注,注意这里的是接口,不是具体的实现类

public LoginService service;

@RequestMapping("/login")//映射地址可随意定义

private String login(String userName,String passWord){

User user=service.login(userName, passWord);

if(user!=null){

return "success";//返回jsp的文件名,不需要前缀和后缀,在springmvc.xml配置了前缀和后缀即可

}

return "fail";

}

}

2.mapper包中放dao层java接口和配置文件:

接口名称与相应的配置文件名一致,配置文件中的id对应接口中的方法名

配置文件的namespace就是对应接口的路径

LoginMapper.java

public interface LoginMapper {

//登陆

public User login(String userName,String passWord);

}

LoginMapper.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.yd.mapper.LoginMapper">

<!-- 登入验证

resultType返回类型,#{0}表示传入的第一个参数

-->

<select id="login" resultType="User">

select * from user where userName=#{0} and passWord=#{1}

</select>

</mapper>

3.pojo包中放置javabean:

User.java

public class User {

private String userName;

private String passWord;

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getPassWord() {

return passWord;

}

public void setPassWord(String passWord) {

this.passWord = passWord;

}

}

4.service包中放业务层接口,同mapper

LoginService.java

public interface LoginService {

//登陆

public User login(String userName,String passWord);

}

5.serviceImpl包中放业务层对应接口的实现类

LoginServiceImpl.java

public class LoginServiceImpl implements LoginService{

@Autowired

public LoginMapper mapper;

@Override

public User login(String userName, String passWord) {

User user=mapper.login(userName, passWord);

return user;

}

}

第三步:创建视图层,在WebContent\WEB-INF 创建文件夹view存放jsp页面(不能通过浏览器直接访问)

1.fail.jsp

2.success.jsp

在WebContent下创建login.jsp(可通过浏览器直接访问)

注意事项:form表单的action必须与控制层相应controller中相应的方法的@RequestMapping的值一致,

 控件的name与对应方法的形参一致。

<%@ 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>login</title>

</head>

<body>

<h1 align="center">Login</h1>

<br>

<form action="login" method="post">

<table width="200" height="100" bgcolor="#000fff" align="center" border="1">

<tr>

<td align="right">account</td>

<td><input type="text" name="userName"></td>

</tr>

<tr>

<td align="right">password</td>

<td><input type="password" name="passWord"></td>

</tr>

<tr>

<td colspan="2" align="right"><input type="submit" value="Login"></td>

</tr>

</table>

</form>

</body>

</html>

第四步:创建一个数据源文件夹,放置SSM相关的配置文件

1.sqlMapConfig.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.yd.pojo"/>

</typeAliases>

<!-- 配置mapper 由于使用spring和mybatis的整合包进行mapper扫描,这里不需要配置了。 必须遵循:mapper.xml和mapper.java文件同名且在一个目录 -->

</configuration>

2.applicationContext-dao.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:p="http://www.springframework.org/schema/p"  

xmlns:context="http://www.springframework.org/schema/context"  

xmlns:mvc="http://www.springframework.org/schema/mvc"  

xsi:schemaLocation="   

  http://www.springframework.org/schema/beans   

  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   

  http://www.springframework.org/schema/context   

  http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  http://www.springframework.org/schema/mvc   

  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

  

<!-- 加载db.properties文件的内容 ,db.properties的key命名要具有一定的特殊规则-->

<context:property-placeholder location="classpath:db.properties"/>

<!-- 配置数据源 -->

<!-- destroy-method="close"的作用是当数据库连接不使用的时候,就把该连接重新放到数据池中,方便下次使用调用. -->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="${mysql.driverClassName}"></property>

<property name="jdbcUrl" value="${mysql.url}"></property>

<property name="user" value="${mysql.username}"></property>

<property name="password" value="${mysql.password}"></property>

<property name="acquireIncrement" value="5"></property>  <!-- 当连接池中的连接用完时,C3P0一次性创建新连接的数目2 -->

<property name="initialPoolSize" value="10"></property>  <!-- 初始化时创建的连接数,必须在minPoolSize和maxPoolSize之间 -->

<property name="minPoolSize" value="5"></property>

<property name="maxPoolSize" value="20"></property>

<!-- 最大空闲时间,超过空闲时间的连接将被丢弃

[需要注意:mysql默认的连接时长为8小时(28800)【可在my.ini中添加 wait_timeout=30(单位秒)设置连接超时】,这里设置c3p0的超时必须<28800] 

-->

<property name="maxIdleTime" value="300"></property>  

<property name="idleConnectionTestPeriod" value="60"></property> <!-- 每60秒检查连接池中的空闲连接 -->

<property name="maxStatements" value="20"></property>  <!-- jdbc的标准参数  用以控制数据源内加载的PreparedStatement数量,但由于预缓存的Statement属 于单个Connection而不是整个连接 -->

</bean>

<!-- 配置SqlSessionFactory -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>

<!-- 加载mybatis的全局配置文件 -->

<property name="dataSource" ref="dataSource" />

</bean>

<!-- 配置mapper扫描器 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 扫描的包,如果要扫描多个包要用半角的,分隔 -->

<property name="basePackage" value="com.yd.mapper"></property>

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>

</bean>

</beans>

3.applicationContext-service.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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:mvc="http://www.springframework.org/schema/mvc"

xsi:schemaLocation="   

  http://www.springframework.org/schema/beans   

  http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   

  http://www.springframework.org/schema/context   

  http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  http://www.springframework.org/schema/mvc   

  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">

<!-- 配置service -->

<bean id="loginService" class="com.yd.serviceImpl.LoginServiceImpl"></bean>

</beans> 

4.applicationContext-transaction.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:p="http://www.springframework.org/schema/p"  

xmlns:context="http://www.springframework.org/schema/context"  

xmlns:mvc="http://www.springframework.org/schema/mvc"  

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.0.xsd   

  http://www.springframework.org/schema/context   

  http://www.springframework.org/schema/context/spring-context-3.0.xsd  

  http://www.springframework.org/schema/mvc   

  http://www.springframework.org/schema/mvc/spring-mvc-3.0.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">

  

<!--事物管理器

对mybatis操作的数据库事物控制,spring使用jdbc的控制

 --> 

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

 <!-- 配置数据源 -->

 <!--在  applicationContext-dao.xml配置了-->

 <property name="dataSource" ref="dataSource"></property>

</bean>

5.springmvc.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 ">

<!--扫描Controller  -->

<context:component-scan base-package="com.yd.controller"></context:component-scan>

<!-- mvc注解驱动器 :使用mvc:annotation-driven可以代替上面2个注解映射器、映射器的配置

mvc注解驱动器会 默认加载很多参数的绑定方法,比如json的转换,默认加载json解析器

实际开发使用mvc注解驱动器 -->

<mvc:annotation-driven>

<mvc:message-converters register-defaults="true">  

<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">  

<property name="supportedMediaTypes" value="application/json"/>  

</bean>  

</mvc:message-converters> 

</mvc:annotation-driven>

<!-- 视图解析器 -->

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<!-- 前缀 /WEB-INF/jsp-->

<property name="prefix" value="/WEB-INF/view/"></property>

<!-- 后缀 -->

<property name="suffix" value=".jsp"></property>

</bean>

</beans>

第五步:配置web.xml文件

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 

http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- 加载Spring容器 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/classes/spring/applicationContext-*.xml</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>

<!-- spirngmvc 前端控制器 -->

<servlet>

<servlet-name>springmvc</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<!-- 通过初始化参数指定springmvc配置文件的位置 -->

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:spring/springmvc.xml</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>springmvc</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

</web-app>

第六步:将工程部署到tomcat,启动服务,通过http://localhost:8080/First/login.jsp链接到登陆页面。

猜你喜欢

转载自15770854359.iteye.com/blog/2343207