SSM框架实现用户查询、注册、登录,2021最新腾讯Java高级面试题总结
2、導(dǎo)入依賴坐標(biāo)
在pom.xml里面導(dǎo)入坐標(biāo),我的pom文件內(nèi)容如下:
<?xml version="1.0" encoding="UTF-8"?><project xmlns=“http://maven.apache.org/POM/4.0.0” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”>
4.0.0
cn.star
myssm
1.0-SNAPSHOT
war
myssm Maven Webapp
http://www.example.com
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
org.aspectj
aspectjweaver
1.6.8
org.springframework
spring-aop
${spring.version}
org.springframework
spring-context
${spring.version}
org.springframework
spring-web
${spring.version}
org.springframework
<artifactId
spring-webmvc
${spring.version}
org.springframework
spring-test
${spring.version}
org.springframework
spring-tx
${spring.version}
org.springframework
spring-jdbc
${spring.version}
junit
junit
4.12
mysql
mysql-connector-java
${mysql.version}
javax.servlet
servlet-api
2.5
provided
javax.servlet.jsp
jsp-api
2.0
provided
jstl
jstl
1.2
log4j
log4j
${log4j.version}
org.slf4j
slf4j-api
${slf4j.version}
org.slf4j
slf4j-log4j12
${slf4j.version}
org.mybatis
mybatis
${mybatis.version}
org.mybatis
mybatis-spring
1.3.0
c3p0
c3p0
0.9.1.2
jar
compile
myssm
maven-clean-plugin
3.1.0
maven-resources-plugin
3.0.2
maven-compiler-plugin
3.8.0
maven-surefire-plugin
2.22.1
maven-war-plugin
3.2.2
maven-install-plugin
2.5.2
maven-deploy-plugin
2.8.2
3、創(chuàng)建java和resources文件夾
在main目錄下面創(chuàng)建java和resources文件夾,并設(shè)置相應(yīng)文件
4、創(chuàng)建類(lèi)和接口文件
在java文件夾下創(chuàng)建類(lèi)文件,所需的類(lèi)文件有以下幾個(gè):
-
存放javabean的 domain 包下的用戶類(lèi):cn.star.domain.Users
-
數(shù)據(jù)訪問(wèn)層 dao 包下的用戶 dao 接口:cn.star.dao.UsersDao
-
業(yè)務(wù)層 service 包下的 UsersService 接口:cn.star.service.UsersService
-
業(yè)務(wù)層 service 包下的 service 實(shí)現(xiàn)類(lèi),繼承service接口:cn.star.service.impl.UsersServiceImpl
-
控制層 controller 包下的用戶控制層UsersController類(lèi):cn.star.controller.UsersController
【1】創(chuàng)建存放 javabean 類(lèi)文件:cn.star.domain.Users
package cn.star.domain;
import java.io.Serializable;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈用戶類(lèi)〉
-
@author OneStar
-
@create 2019/11/8
-
@since 1.0.0
*/
public class Users implements Serializable {
private Integer id;
private String username;
private String PASSWORD;
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
@Override
public String toString() {
return “Users{” +
“id=” + id +
“, username='” + username + ‘’’ +
“, PASSWORD='” + PASSWORD + ‘’’ +
‘}’;
}
}
【2】創(chuàng)建數(shù)據(jù)訪問(wèn)層 UsersDao 接口:cn.star.dao.UsersDao
這里只需要編寫(xiě)接口就可以了,框架會(huì)幫我們生成一個(gè)代理對(duì)象
package cn.star.dao;
import cn.star.domain.Users;
import java.util.List;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈數(shù)據(jù)訪問(wèn)層 UsersDao 接口〉
-
@author OneStar
-
@create 2019/11/8
-
@since 1.0.0
*/
public interface UsersDao {
//查詢所有用戶
public List findUsers();
//用戶注冊(cè)
public void insertUsers(Users users);
//用戶登錄
public Users login(Users users);
}
【3】創(chuàng)建業(yè)務(wù)層 UsersService 接口:cn.star.service.UsersService
package cn.star.service;
import cn.star.domain.Users;
import java.util.List;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈業(yè)務(wù)層接口〉
-
@author OneStar
-
@create 2019/11/9
-
@since 1.0.0
*/
public interface UsersService {
//查詢所有用戶
public List findUsers();
//用戶注冊(cè)
public void insertUsers(Users users);
//用戶登錄
public boolean login(Users users);
}
【4】Service 實(shí)現(xiàn)類(lèi):cn.star.service.impl.UsersServiceImpl
繼承 UsersService 接口
package cn.star.service.impl;
import cn.star.domain.Users;
import cn.star.service.UsersService;
import java.util.List;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈Service 實(shí)現(xiàn)類(lèi)〉
-
@author OneStar
-
@create 2019/11/9
-
@since 1.0.0
*/
public class UsersServiceImpl implements UsersService {
@Override
public List findUsers() {
System.out.println(“業(yè)務(wù)層:查詢用戶”);
return null;
}
@Override
public void insertUsers(Users users) {
System.out.println(“業(yè)務(wù)層:用戶注冊(cè)”);
}
@Override
public boolean login(Users users) {
System.out.println(“業(yè)務(wù)層:用戶登錄”);
}
}
【5】創(chuàng)建用戶控制層UsersController類(lèi):cn.star.controller.UsersController
5、創(chuàng)建配置文件
在resources文件夾下創(chuàng)建多個(gè)配置文件,主要有:
-
spring相關(guān)配置:spring.xml
-
springmvc相關(guān)配置:spring-mvc.xml
-
mybatis相關(guān)配置:mybatis.xml
-
日志相關(guān)配置:log4j.properties
【1】spring.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: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">
【2】spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns=“http://www.springframework.org/schema/beans”
xmlns:mvc=“http://www.springframework.org/schema/mvc”
xmlns:context=“http://www.springframework.org/schema/context”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
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">
【3】mybatis.xml
<?xml version="1.0" encoding="UTF-8"?>【4】log4j.properties
Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=info, CONSOLE, LOGFILE
Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=d:\axis.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r [%15.15t] %-5p %30.30c %x - %m\n
【5】目錄結(jié)構(gòu)
至此,整個(gè)目錄結(jié)構(gòu)如下
三、整合框架編寫(xiě)
========
1、編寫(xiě)spring框架
【1】配置注解掃描
在spring.xml中配置注解掃描,加上以下配置:
<context:component-scan base-package=“cn.star”>
<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller” />
</context:component-scan>
【2】為service實(shí)現(xiàn)類(lèi)添加注解
在UsersServiceImpl類(lèi)中添加注解,把service交給IOC容器管理
@Service(“usersService”)
public class UsersServiceImpl implements UsersService {…}
【3】測(cè)試spring框架
這里只做用戶查詢測(cè)試,創(chuàng)建測(cè)試類(lèi):cn.star.test.springtest
package cn.star.test;
import cn.star.service.UsersService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈測(cè)試類(lèi)〉
-
@author OneStar
-
@create 2019/11/11
-
@since 1.0.0
*/
public class springtest {
@Test
public void Test(){
//加載配置文件
ApplicationContext ac = new ClassPathXmlApplicationContext(“classpath:spring.xml”);
//獲取對(duì)象
UsersService us = (UsersService) ac.getBean(“usersService”);
//調(diào)用方法
us.findUsers();
}
}
運(yùn)行,打印以下信息則搭建成功:
2、編寫(xiě) SpringMVC 框架
【1】配置 web.xml
-
配置前端控制器
-
加載spring-mvc.xml配置文件
-
啟動(dòng)服務(wù)器,創(chuàng)建Servlet
-
配置過(guò)濾器解決中文亂碼
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
/
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
【2】配置 spring-mvc.xml
-
開(kāi)啟注解掃描,只掃描 controller
-
配置視圖解析器
-
過(guò)濾靜態(tài)資源
-
開(kāi)啟SpringMVC注解支持
<context:component-scan base-package=“cn.star”>
<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller” />
</context:component-scan>
<mvc:resources location=“/css/” mapping=“/css/**” />
<mvc:resources location=“/images/” mapping=“/images/**” />
<mvc:resources location=“/js/” mapping=“/js/**” />
mvc:annotation-driven/
【3】測(cè)試SpringMVC
-
編寫(xiě)用戶控制層
-
編寫(xiě)index.jsp頁(yè)面
-
編寫(xiě)跳轉(zhuǎn)頁(yè)面
1. 編寫(xiě)UsersController用戶控制層
package cn.star.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈用戶控制層〉
-
@author OneStar
-
@create 2019/11/9
-
@since 1.0.0
*/
@Controller
@RequestMapping(“/users”)
public class UsersController {
@RequestMapping(“/findUsers”)
public String findUsers(){
System.out.println(“表現(xiàn)層:查詢用戶”);
return “Users”;
}
}
2. 編寫(xiě)index.jsp頁(yè)面
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
Title查詢用戶
3. 編寫(xiě)跳轉(zhuǎn)頁(yè)面
在 WEB-INF 目錄下新建 pages 目錄,pages 目錄下創(chuàng)建 Users.jsp 頁(yè)面,編寫(xiě) Users.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” isELIgnored=“false” %>
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
Title查詢所有用戶
4. 部署Tomcat項(xiàng)目并測(cè)試
部署好后運(yùn)行,點(diǎn)擊測(cè)試查詢鏈接,跳轉(zhuǎn)到查詢用戶界面,SpringMVC配置成功
3、Spring 整合 SpringMVC 框架
使用spring框架整合springmvc框架,目的就是能夠使用 controller 層方法調(diào)用 service 業(yè)務(wù)層方法,那要如何實(shí)現(xiàn)呢?分析如下:
瀏覽器發(fā)送請(qǐng)求,通過(guò)web.xml中配置的前端控制器加載springmvc.xml配置文件
在springmvc.xml配置文件中配置Spring的監(jiān)聽(tīng)器,默認(rèn)只加載WEB-INF目錄下的spring.xml配置文件
將service注入controller,調(diào)用service對(duì)象的方法進(jìn)行測(cè)試
【1】配置web.xml
配置Spring的監(jiān)聽(tīng)器和文件路徑
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:spring.xml
【2】service注入controller
修改 UsersController 類(lèi),將service注入controller,調(diào)用service對(duì)象的方法進(jìn)行測(cè)試,修改后如下:
package cn.star.controller;
import cn.star.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
/**
-
〈一句話功能簡(jiǎn)述〉
-
〈用戶控制層〉
-
@author OneStar
-
@create 2019/11/9
-
@since 1.0.0
*/
@Controller
@RequestMapping(“/users”)
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping(“/findUsers”)
public String findUsers(){
System.out.println(“表現(xiàn)層:查詢用戶”);
//調(diào)用service對(duì)象的方法進(jìn)行測(cè)試
usersService.findUsers();
return “Users”;
}
}
【3】測(cè)試運(yùn)行
點(diǎn)擊測(cè)試查詢鏈接后,在后臺(tái)打印一下信息,說(shuō)明spring成功整合springmvc
4、編寫(xiě) mybatis 框架
【1】配置mybatis.xml配置文件
在configuration標(biāo)簽內(nèi)添加以下配置:
【2】編寫(xiě)UserDao類(lèi)
public interface UsersDao {
//查詢所有用戶
@Select(“select * from users”)
public List findUsers();
//用戶注冊(cè)
@Insert(“INSERT INTO USERS (username,PASSWORD) VALUES(#{username},#{PASSWORD})”)
總結(jié)
以上是生活随笔為你收集整理的SSM框架实现用户查询、注册、登录,2021最新腾讯Java高级面试题总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C#建立控件数组,实现控件批量操作
- 下一篇: 第六届全国信息技术应用水平大赛Java组