1.准备测试数据库
2.根据数据库准备测试类
pojo
public class UserInfo {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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.DAO
public interface IUserInfoDao{
UserInfo doLogin(UserInfo userInfo);
}
4.整合 Spring + Mybatis – ApplicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="maxPoolSize" value="30"></property>
<property name="minPoolSize" value="2"></property>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.zjitc.pojo"></property>
<property name="mapperLocations" value="classpath:mapper/*.xml"/>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<props>
<prop key="helperDialect"></prop>
<prop key="reasonable"></prop>
</props>
</property>
</bean>
</array>
</property>
</bean>
<!-- 自动生成dao,mapper-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zjitc.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 扫描全部包-->
<context:component-scan base-package="com.zjitc"></context:component-scan>
<!-- 配置事务-->
<!-- 5.配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 6.开启事务注解-->
<tx:annotation-driven></tx:annotation-driven>
</beans>
5.编写mapper.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.zjitc.dao.IUserInfoDao" >
<!--字段不一致-->
<!-- <select id="selectUser" resultMap="UserMap">-->
<!-- select * from user-->
<!--</select>-->
<!-- 设置结果的映射类型-->
<!-- <resultMap id="UserMap" type="User">-->
<!-- 一般通过id标签来映射主键-->
<!-- column = 数据库的列名-->
<!-- property = 结果集对应的数据库列名的映射名-->
<!-- –>-->
<!-- <id column="id" property="id"/>-->
<!-- <result column="name" property="name"/>-->
<!-- <result column="pwd" property="password"/>-->
<!-- </resultMap>-->
<select id="doLogin" parameterType="UserInfo" resultType="UserInfo">
select * from user where username = #{username} and password = #{password}
</select>
</mapper>
注意namespace得路径一定要对应上方法名要和dao层对应
6. 编写Service层
接口
public interface IUserInfoService {
UserInfo doLogin(UserInfo userInfo);
}
实现类
public class IUserInfoServiceImpl implements IUserInfoService {
@Autowired
private IUserInfoDao iUserInfoDao;
@Override
public UserInfo doLogin(UserInfo userInfo) {
UserInfo user = iUserInfoDao.doLogin(userInfo);
return user;
}
}
7.整合 Spring-MVC.xml 和 web.xml
Spring-MVC.xml
<beans 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" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans">
<!-- 扫描位置-->
<context:component-scan base-package="com.zjitc.controller"></context:component-scan>
<!-- 注解支持-->
<!-- 注解的处理器映射器的简化配置 -->
<!-- <mvc:annotation-driven></mvc:annotation-driven>-->
<!-- 映射处理器适配处理器-->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"></bean>
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"></bean>
<!-- 视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀-->
<property name="prefix" value="/pages/"></property>
<!-- 后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
web.xml
<web-app version="3.1" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<!-- 加载类路径配置文件 -->
<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>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- 配值解决中文乱码-->
<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>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern> /*</url-pattern>
</filter-mapping>
<!-- 前端控制器,指定加载spingmvc-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
8.导入页面/view
9.编写Controller
public class UserInfoController {
@Autowired
private IUserInfoService iUserInfoService;
@RequestMapping("/doLogin.do")
public String doLogin(UserInfo user, HttpSession session, HttpServletRequest request){
UserInfo userInfo = iUserInfoService.doLogin(user);
if (userInfo!=null){
if (user.getPassword().equals(userInfo.getPassword())){
session.setAttribute("user",userInfo);
return "/main";
}
}
request.setAttribute("msg","用户名密码错误");
return "/login";
}
}
10.测试