Spring+Mybatis之登入功能demo

來源:互聯網
上載者:User

標籤:demo   images   印象   前端   ==   int   enc   -name   .sql   

  其實工作之後就沒有用過Spring+Mybatis的架構了,因為公司有一個自己開發的架構,講道理,其實這個與Spring+Mybatis整合很是神似。當然效能上還是比不上Spring+Mybatis所整合的架構的。之前學習的時候,是學的Spring+Mybatis架構,其實已經忘記很多了,今天翻開之前的代碼看了一下了,現在順便做個總結加深一下自己的印象吧。

  其實一個系統的登入還是比較重要的,此處也只是寫一個簡單的demo。對於一個新的項目,當然所有的東西都要從頭開始了。首先需要建立以下幾個package

其他的先不說,先將所需要的jar包拷貝到lib檔案夾下面

此處需要注意的是,由於我串連資料庫是用的是c3p0串連池。當然也可以用DBCP串連池。

可以先寫好一個設定檔用於串連資料庫的,我這串連的資料不是oracle,而是MySQL,該設定檔的尾碼名為.properties

該設定檔資訊,需要以這種索引值對的形式存在,因為在spring-mvc.xml檔案中需要讀取該設定檔,也就是根據其key值來獲得其value值

然後先配置一下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">
<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>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>

該web.xml檔案中就是配置Spring前端單一控制器的。就也是spring-mvc.xml。該項目的所以servlet請求也都是尾碼名為.do的

接下來就是配置spring-mvc.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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:util="http://www.springframework.org/schema/util"
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.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<context:component-scan base-package="com"></context:component-scan>
<!-- 讀取資料庫設定檔 -->
<util:properties id="jdbc" location="classpath:db.properties"></util:properties>
<!-- 設定資料庫串連池 -->
<bean id="c3p0" 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.user}"></property>
<property name="password" value="#{jdbc.password}"></property>
<property name="minPoolSize" value="#{jdbc[‘min.pool.size‘]}"></property>
<property name="maxPoolSize" value="#{jdbc[‘max.pool.size‘]}"></property>
</bean>

<!-- 配置SqlSessionFactoryBean 添加兩個屬性dataSource以及mapperLocations -->
<bean id="SqlSession" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="c3p0"></property>
<property name="mapperLocations" value="classpath:com/entity/*.xml"></property>
</bean>
<!-- 配置MapperFactoryBean -->
<!-- <bean id="mapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="SqlSession"></property>
<property name="mapperInterface" value="com.dao.EmpMapper"></property>
</bean>
-->
<!-- 配置MapperScannerConfigurer指定掃描一個包下面的所有介面對象,從而註冊成一個MapperFactoryBean對象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dao"></property>
<!-- <property name="sqlSessionFactory" ref="SqlSession"></property> -->
<!--可以指定掃描包裡面的某些介面產生MapperFactoryBean -->
<property name="annotationClass"
value="com.annotation.MybatisRegist"></property>

</bean>

<!-- 組件掃描 -->
<context:component-scan base-package="com"></context:component-scan>
<!-- 開啟映射註解路徑 -->
<mvc:annotation-driven/>
<!-- 配置視圖解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"></property>
<property name="suffix" value=".jsp"></property>
</bean>

<!-- 配置攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!-- 指定的請求都會被攔截 -->
<mvc:mapping path="/*/*"/>
<!-- 指定的請求不會被攔截 -->
<mvc:exclude-mapping path="/login/*"/>
<bean class="com.controller.DemoInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
</beans>

 

關於spring-mvc.xml 檔案的配置,我也都寫了注釋

 設定檔都準備好了,對於登入操作是需要涉及到資料庫中的表的,有表的話,那麼在後台就需要建立一個屬性與表欄位對應的實體類

該實體類放在com.entity 包中

然後再建立一個登入頁面吧,該頁面放在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">
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<title>Insert title here</title>
<style type="text/css">
h1,h2,h3,h4,form,p{margin: 0px;padding: 0px}
.form{
margin: 0px auto;
padding: 2px;
border: 2px solid gray;
width: 180px;
float:left;
}
.form h2{
text-align:center;
background: black;
color: white;
}
.form p{
background: #ddd;
padding: 3px;
}
.form p input{
width: 120px;
}

.form h3 {
background: #ddd;
text-align:center;
}
.form #p{
background: #ddd;
color:red;
}

</style>
</head>
<body>
<div class="form">
<form action="login.do" method="post">
<h2>登入</h2>
<p>使用者:<input name="user_name" value="${u.user_name}"/></p>
<p>密碼:<input type="password" name="password"></p>
<p id="p"> ${messag}</p>
<h3><input type="submit" value="登入"></h3>
</form>
</div>

</body>
</html>

該頁面需要注意一點的是:需要引入jstl標準標籤庫

成型的登入頁面

現在資料庫連接,spring相關的配置,網頁,實體類都寫好了,那麼接下來就是寫後台代碼了。

在寫後台代碼之前需要先寫一個類,用於為介面做註解映射的,該類放在com.annotation包下

有個疑問:該註解怎麼用呢?

答:在每個介面上都加上該註解標記

建立一個介面,放在com.dao包下面

看該介面就清楚,其實登入的本質就是根據使用者名稱來查詢

接下來就需要寫一下登入的商務邏輯類,該類放在com.service

package com.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.dao.UserMapper;
import com.entity.User;
@Service
public class UserService {
@Resource
UserMapper mapperu;

public User login(String user_name,String password) throws EmptyParamException, ErrorParamException{
System.out.println(user_name);
if(user_name.equals("")){

throw new EmptyParamException("使用者名稱不可為空");
//return null;
}
if(password.equals("")){
throw new EmptyParamException("密碼不可為空");
//return null;
}
User user=mapperu.findBy(user_name);
if(user==null){
throw new ErrorParamException("使用者名稱錯誤");

}
if(user.getPassword().equals(password)){
return user;
}
throw new ErrorParamException("密碼錯誤");

}
public User findby(String user_name){
User user=mapperu.findBy(user_name);
return user;
}

}

 需要注意的是,該類中拋出了兩個異常,因此需要寫兩個異常類,也放在com.service包中

 

這些都寫好了,那麼就寫一下控制層的代碼,該代碼寫在com.controller包下

首先在寫登入方法前,需要寫上需要的註解注入標記

此處我並不是讓使用者直接存取一個登入頁面的,而是通過一個請求來訪問的

該方法用於使用者在地址欄輸入然後跳到登入介面

進登入介面後,使用者填好登入資訊,按登入按鈕,則會發送一個尾碼名.do的請求,則在控制器中找到該請求的方法

登入成功則會跳到需要的頁面,沒有登入成功則會發送一個提示給前台

最後寫一下,登入的查詢語句,該語句寫在一個與使用者實體類所對應的xml檔案中,該檔案也建立在com.entity

 

 需要注意的是:該sql語句中的ID與介面的方法名所一致,該檔案中namespace要註明該sql語句所對應的介面

感覺架構配置還是比較繁瑣的,總結下來還是有很多東西的。但確實省了不少代碼。

Spring+Mybatis之登入功能demo

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.