1
首先在eclipse中建立一个Dynamic Web project
2
建立如下包结构
3
在实现SSM整合时, Mybati实现Dao开发有两种方式,一种是原始Dao开发,另一种是Mapper动态代理的方式,本案例采用Mapper动态代理的方式
功能实现逻辑为 :UserMapper.xml(配置文件)——> User.java(实体类)——>UserMapper.java(动态接口)——>UserServiceImpl(业务逻辑)——>UserService(接口)——>UserController——>login.jsp
4
数据库采用MySql ,创建数据库为:tab_ssm ,表名为:User , 三个字段分别为:id username,password
5
开始写相关配置文件 :
(1) applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans /spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!--用引入数据库配置文件-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!--2.配置mybatis的SqlSession的工厂: SqlSessionFactoryBean dataSource:引用数据源 -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<!-- 核心配置文件的位置 -->
<property name="configLocation" value="classpath:sqlMapConfig.xml" />
</bean>
<!-- 3. 自动扫描mybatis映射文件和接口的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 基本包 -->
<property name="basePackage" value="cn.itcast.mapper" />
</bean>
<!--4.配置事务管理器 -->
<bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--5.开启注解进行事务管理 transaction-manager:引用上面定义的事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
( 2) db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/tab_ssm?characterEncoding=utf-8
jdbc.username=root
jdbc.password=123
(3)log4J.properties文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
(4)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: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-4.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!--1 扫描@Controler@Service -->
<context:component-scan base-package="cn.itcast" />
<!--2. 开启注解 -->
<mvc:annotation-driven />
<!--3视图解释器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
(5)sqlMaperConfig.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="cn.itcast.entity"/>
</typeAliases>
</configuration>
(6)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>springmvc-mybatis</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- Spring监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 处理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>*.action</url-pattern>
</filter-mapping>
<!-- 前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认找 /WEB-INF/[servlet的名称]-servlet.xml -->
<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>*.action</url-pattern>
</servlet-mapping>
</web-app>
6 下面开始实现真正的业务逻辑
(1)编写UserMapper.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="cn.itcast.mapper.UserMapper">
<select id="findUser" parameterType="cn.itcast.entity.User" resultType="cn.itcast.entity.User">
select * from User where username=#{username} and password=#{password}
</select>
</mapper>
( 2 ) 编写实体类 User.java
public class User {
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
( 3 ) UserMapper.java
package cn.itcast.mapper;
import cn.itcast.entity.User;
public interface userMapper {
/**
* 用户登录
* @param user
* @return
*/
public User findUser(User user);
}
( 4 ) UserServiceImpl (具体业务实现)
package cn.itcast.serviceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.itcast.entity.User;
import cn.itcast.service.UserService;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private cn.itcast.mapper.userMapper userMapper;
@Override
public User login(User user) {
return userMapper.findUser(user);
}
}
( 5 ) UserService.java(接口)
package cn.itcast.service;
import cn.itcast.entity.User;
public interface UserService
{
public User login(User user);
}
(6) UserController.java
package cn.itcast.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import cn.itcast.entity.User;
import cn.itcast.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping(value="/tologin.action")
public String tologin() {
return "login";
}
@RequestMapping(value="/login.action")
public String findUser(User user, Model model) {
User u = userService.login(user);
if (u!= null) {
model.addAttribute("u", u);
return "success";
}
return "failure";
}
}
( 7 ) 登录页面的编写 login.jsp
<form action="${pageContext.request.contextPath}/login.action" method="POST">
<table border="1" width="35%">
<tr>
<td>帐号</td>
<td><input type="text" name=username></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name=password></td>
</tr>
<tr>
<td><input type="submit" value="提交"></td>
<td><input type="reset" value="重置"></td>
</tr>
</table>
</form>
登录操作到此结束 ,希望能对新手有帮助,我本人能力有限,不足之处还望指出,愿大家一起进步。谢谢
.