1、建立工程,整合jar包
2、資料庫(oracle)鍵表,包含id,name,password三個字段
3、配置db.propertise檔案,用于配置資料源
url=jdbc:oracle:thin:@localhost:1521:XE
user=oracle
passwd=123
driver=oracle.jdbc.OracleDriver
3、配置spring相關檔案,取名為spring.xml,放在src目錄下,詳見配置檔案
<?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: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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--引入propertise檔案 -->
<!--傳統方式引入 -->
<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> -->
<!-- <property name="locations" value="classpath:db.properties"></property> -->
<!-- </bean> -->
<!--簡化方式 -->
<context:property-placeholder location="classpath:db.properties"/>
<!--1.配置資料源:c3p0 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}" />
<property name="jdbcUrl" value="${url}" />
<property name="user" value="${user}" />
<property name="password" value="${passwd}" />
</bean>
<!--2.配置mybatis的SqlSession的工廠: SqlSessionFactoryBean dataSource:引用資料源 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="typeAliasesPackage" value="com.ssm.bean" />
</bean>
<!-- 3. 自動掃描mybatis映射檔案和接口的包 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssm.dao"></property>
</bean>
<!--4.配置事務管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--5.開啟注解進行事務管理 transaction-manager:引用上面定義的事務管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
4、配置springmvc相關檔案,取名為springmvc.xml,同樣放在src目錄下
<?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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--1.開啟Springioc 自動掃描注解包 -->
<context:component-scan base-package="com.ssm"/>
<!--2. 開啟注解 -->
<mvc:annotation-driven />
<!--3.配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--4.注解映射器(可省) -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"></bean> -->
<!--5.配置擴充卡(不需時可省) -->
<!-- <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
在業務方法的傳回值和權限之間使用@ResponseBody注解表示傳回值對象需要轉成JSON文本
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/>
</list>
</property>
</bean> -->
</beans>
5、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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>ssm</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!--Spring核心監聽器 -->
<!--在伺服器啟動時加載Spring容器,且隻會加載一次 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--配置Springmvc核心控制器 -->
<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:springmvc.xml</param-value>
</init-param>
</servlet>
<!-- Map all requests to the DispatcherServlet for handling -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<!--配置由Spring 提供的針對中文亂碼的編碼過濾器 -->
<!-- 編碼過濾器 -->
<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>
</web-app>
6、持久層功能實作
userDao.java (映射接口)
package com.ssm.dao.user;
import org.apache.ibatis.annotations.Param;
import com.ssm.bean.User;
/**
* 持久層映射接口
* @author Nocol
*
*/
public interface UserDao {
//添加使用者
public void addUser(User user);
//根據使用者名和密碼查詢使用者
//注解的兩個參數會自動封裝成map集合,括号内即為鍵
public void findUserByNameAndPwd(@Param("name")String name, @Param("password")String password);
}
UserDao.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.ssm.dao.user.UserDao">
<!--注意sql語句沒有分号結尾 -->
<insert id="addUser" parameterType="User">
insert into t_user(id,name,password) values(#{id},#{name},#{password})
</insert>
<!--注意這裡的參數類型是parameterType而不是parameterMap,因為傳回的是單個類型 -->
<select id="findUserByNameAndPwd" parameterType="map" resultType="User">
select t.name,t.password from t_user t where name=#{name} and password=#{password}
</select>
</mapper>
7、業務層功能實作
UserService.java
package com.ssm.service;
import com.ssm.bean.User;
public interface UserService {
//使用者注冊
void regist(User user);
//使用者登入
void login(String name, String password);
}
UserServiceImpl.java
package com.ssm.service.Imp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ssm.bean.User;
import com.ssm.dao.user.UserDao;
import com.ssm.service.UserService;
/**
* 業務層
* @author Nocol
*
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void regist(User user) {
// TODO Auto-generated method stub
userDao.addUser(user);
}
@Override
public void login(String name, String password) {
// TODO Auto-generated method stub
userDao.findUserByNameAndPwd(name,password);
}
}
8、控制層功能實作
UserAction.java
package com.ssm.controller.user;
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 com.ssm.bean.User;
import com.ssm.service.UserService;
/**
* 控制層
* @author Nocol
*
*/
@Controller
@RequestMapping("/user")
public class UserAction {
//注入Service
@Autowired
private UserService userService;
@RequestMapping("regist")
public String regist(User user,Model model){
System.out.println("使用者注冊:"+user.getName()+user.getPassword());
user.setId(1);
userService.regist(user);
model.addAttribute("msg", "注冊成功");
//注冊成功後跳轉success.jsp頁面
return "success";
}
@RequestMapping("login")
public String login(String name,String password,Model model){
System.out.println("使用者登入:"+name+password);
/*Map<String, String> map=new LinkedHashMap<String,String>();
map.put("name", user.getName());
map.put("password", user.getPassword());*/
userService.login(name,password);
model.addAttribute("msg", "登入成功");
return "success";
}
}
9、jsp頁面實作
regist.jsp
<%@ 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>使用者注冊</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/regist.action">
<table >
<tr>
<td>使用者名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="注冊"></td>
</tr>
</table>
</form>
</body>
</html>
login.jsp
<%@ 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>使用者登入</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/user/login.action">
<table >
<tr>
<td>使用者名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密碼</td>
<td><input type="text" name="password"></td>
</tr>
<tr>
<td><input type="submit" value="登入"></td>
</tr>
</table>
</form>
</body>
</html>
success.jsp
<%@ 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>登入成功</title>
</head>
<body>
${msg }</br>
<a href="${pageContext.request.contextPath }/login.jsp" target="_blank" rel="external nofollow" >去登入</a>
</body>
</html>