<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/register.jspwelcome-file>
welcome-file-list>
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springconfig.xmlparam-value>
init-param>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
<filter>
<filter-name>CharEncodingFilterfilter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
filter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
4.配置db.properties
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@10.10.21.3:1521:orcl
#jdbc.username=scott
#jdbc.password=tiger
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
5.配置springconfig.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"
xmlns:task="http://www.springframework.org/schema/task"
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.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-4.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.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>
<property name="password" value="${jdbc.password}">property>
<property name="maxActive" value="20">property>
<property name="maxIdle" value="20">property>
bean>
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="mapperLocations"
value="classpath:com/qut/mapper/*.xml">property>
bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.qut.mapper">property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">property>
bean>
<context:component-scan base-package="com.qut">context:component-scan>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/">property>
<property name="suffix" value=".jsp">property>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource">property>
bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
<mvc:annotation-driven />
beans>
6.在com.qut.pojo包下面建一个User类与数据库里面的表对应
package com.qut.pojo;
public class User {
private int id;
private String name;
private String pwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
7.在com.qut.mapper包下面写一个mapper接口UserMapper.java,定义mapper接口
此方法即mapper接口开发方法,只需定义mapper接口,不用编写mapper接口实现类。只需要在spring配置文件中定义一个mapper扫描器,自动扫描包中的mapper接口生成代代理对象
注意mapper.xml的文件名和mapper的接口名称保持一致,且放在同一个目录
package com.qut.mapper;
import com.qut.pojo.User;
public interface UserMapper {
/**
* 插入用户信息
* */
public int insert(User user);
/**
* 查找用户
* @param user
* @return
*/
public User findUser(User user);
}
8.配置mapper扫描器在com.qut.mapper包下新建UserMapper.xml
如果将mapper.xml和mapper接口的名称保持一致且放在一个目录 则不用在springconfig.xml中进行配置
ResultMap和ResultType的差别、ParameterMap和parameterType的差别
<mapper namespace="com.qut.mapper.UserMapper">
<insert id="insert" parameterType="com.qut.pojo.User">
insert into user values(null,#{name},#{pwd})
insert>
<select id="findUser" parameterType="com.qut.pojo.User"
resultType="com.qut.pojo.User"
>
select * from user where name=#{name} and pwd=#{pwd}
select>
mapper>
8.定义service接口
在com.qut.service下新建一个接口UserService.java
package com.qut.service;
import com.qut.pojo.User;
public interface UserService {
public int register(User user);
public User login(User user);
}
9.实现service接口
在com.qut.service.impl包下新建一个UserServiceImpl.java
package com.qut.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.qut.mapper.UserMapper;
import com.qut.pojo.User;
import com.qut.service.UserService;
@Service
@Transactional(rollbackFor=Exception.class)
public class UserServiceImpl implements UserService{
@Autowired
private UserMapper userMapper;
public int register(User user) {
//调用dao操作数据库
return userMapper.insert(user);
}
public User login(User user) {
return userMapper.findUser(user);
}
}
10.编写控制器
在com.qutcontroller包下新建一个UserController.java
package com.qut.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.qut.pojo.User;
import com.qut.service.UserService;
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/register")
public ModelAndView register(User user){
ModelAndView view = new ModelAndView();
int res = userService.register(user);
if(res!=1){
//回到注册界面
view.setViewName("register");
}else{
//登录界面
view.setViewName("login");
}
return view;
}
@RequestMapping("/login")
public ModelAndView login(User user){
User u = userService.login(user);
ModelAndView view = new ModelAndView();
if(u!=null){
view.setViewName("main");
}else{
//添加错误信息
view.setViewName("login");
}
return view;
}
}
11.编写jsp显示页面