web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-mvc.xml:
<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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 -->
<context:component-scan base-package="com.controller.*" />
<!--避免IE执行AJAX时,返回JSON出现下载文件 -->
<bean id="mappingJacksonHttpMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
</list>
</property>
</bean>
Spring-mybatis.xml:
<!-- 自动扫描 -->
<!-- <context:component-scan base-package="com.services.*" /> -->
<!-- 引入配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties" />
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="${initialSize}"></property>
<!-- 连接池最大数量 -->
<property name="maxActive" value="${maxActive}"></property>
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="${maxIdle}"></property>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="${minIdle}"></property>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="${maxWait}"></property>
</bean>
<!-- 第一种 spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<!-- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
自动扫描mapping.xml文件
<property name="mapperLocations" value="classpath*:mapping/*.xml"></property>
</bean> -->
<!--第二种 mybatis-spring整合 手动配置mybatis配置文件-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.DAO" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
mybatis.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>
<!--type:对象类型 alias:对象别名 -->
<typeAlias type="com.model.User" alias="User"/>
</typeAliases>
<!--配置mybatis映射文件 -->
<mappers>
<mapper resource="com/mapper/UserMapper.xml"/>
</mappers>
</configuration>
DAO:
package com.DAO;
import com.model.User;
public interface UserDao {
User findUserByName(String username,String pwd);
}
mapper:
<mapper namespace="com.DAO.UserDao">
<!-- 新增 -->
<insert id="saveUser" parameterType="com.cn.ssm.pojo.User" >
insert into t_user(user_name,user_age) values (#{username},#{age})
</insert>
<!-- 修改 -->
<update id="updateUser" parameterType="com.cn.ssm.pojo.User" >
update t_user set user_name=#{username},user_age=#{age} where user_id=#{id}
</update>
<!-- 删除 -->
<delete id="deleteUser" parameterType="int">
delete from t_user where user_id=#{id}
</delete>
<!-- 根据id查找单个用户 -->
<select id="findUserById" parameterType="java.lang.Integer" resultType="com.cn.ssm.pojo.User">
select id,user_name,age from user_t where id=#{id}
</select>
<!-- 查询所有 -->
<select id="findAll" resultType="com.cn.ssm.pojo.User">
select user_id id,user_name userName,user_age age from t_user
</select>
<!-- 根据用户名和密码查询用户 -->
<select id="findUserByName" parameterType="String" resultType="com.model.User">
<!-- 只传一个参数到sql语句时,可以直接写参数名,当传多个参数时,需用0,1,2...,或者在dao层使用@Param注解-->
select * from userinfo where UserName=#{0} AND PassWord=#{1}
</select>
services:
package com.services;
import com.model.User;
public interface IUserService {
public User findUserByName(String username,String pwd);
}
services.implents:
@Service
@Transactional
public class UserServiceImpl implements IUserService{
@Autowired
public UserDao UserMapper;
public User findUserByName(String name,String password){
return this.UserMapper.findUserByName(name, password);
}
}
Controller:
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("longin")
public void checkLogin(HttpServletRequest request,HttpServletResponse response){
String username=request.getParameter("name");
String password=request.getParameter("password");
response.setCharacterEncoding("utf-8");
System.out.println(username+","+password);
User user=userService.findUserByName(username, password);
HttpSession session=request.getSession();
if(user!=null){
session.setAttribute("user", user);
try {
//response.sendRedirect("../index2.jsp");
response.getWriter().write("登录失败");
} catch (IOException e) {
e.printStackTrace();
}
}else{
try {
response.getWriter().write("登录失败");
} catch (IOException e) {
// TODO Auto-generated catch block
jsp:
<body>
<form action="user/longin">
<label>用户名:<input type="text" name="name"></label>
<label>密码:<input type="password" name="password"></label>
<input type="submit" value="登录...">
</form>
<button onclick="login_ip()">IP登录</button>
</body>