一個簡單的管理系統 Springmvc 一不留神就躺坑

前言

好久沒有用springmvc寫項目了,抽時間寫一個簡單的springmvc項目

是什麼(what)爲什麼(why)怎麼做(how)

1.讀書破萬卷下筆如有神(理清思路,知識儲備和前期整理)

2.遇到問題,多方面尋找解決的思路及方法

3.總結,回顧寫過的坑

1.初始化數據庫

數據庫的話可以選擇mysql,oracle 最近oracle用得比較多所以選用oracle

(1).查看數據文件存儲路徑

select * from dba_data_files
(2).創建表空間

1 create tablespace TS_MY_DATA
2 DATAFILE ‘*******\TS_MY_DATA.DBF’
3 SIZE 2G AUTOEXTEND ON;

(3).創建自己的用戶對象

create user 用戶名 identified by 用戶密碼

default tablespace TS_MY_DATA

ACCOUNT UNLOCK;

–system用戶下賦權

grant connect to 用戶名 ;

grant resource to 用戶名 ;

grant create procedure to 用戶名 ;

grant create view to 用戶名 ;

grant debug connect session to 用戶名 ;

grant execute any procedure to 用戶名 ;

grant select any table to 用戶名 ;

grant insert any table to 用戶名 ;

grant update any table to 用戶名 ;

grant delete any table to 用戶名 ;

grant drop any table to 用戶名 ;

grant unlimited tablespace to 用戶名 ;

grant dba to 用戶名 ;

grant create any table to 用戶名 ;

grant analyze any to 用戶名 ;

(4).登陸自己的用戶對象,創建表

create table USER_MSG

(

id VARCHAR2(64) not null,

create_timestamp NUMBER(32),

updata_timestamp NUMBER(32),

username VARCHAR2(200),

password VARCHAR2(200),

sex VARCHAR2(2),

age VARCHAR2(5),

address VARCHAR2(100),

phone VARCHAR2(13),

status VARCHAR2(1) default 0 not null,

salt VARCHAR2(10)

)

tablespace TS_MY_DATA

pctfree 10

initrans 1

maxtrans 255;

– Add comments to the columns

comment on column USER_MSG.id

is ‘用戶id’;

comment on column USER_MSG.create_timestamp

is ‘創建時間’;

comment on column USER_MSG.updata_timestamp

is ‘修改時間’;

comment on column USER_MSG.username

is ‘姓名’;

comment on column USER_MSG.password

is ‘密碼’;

comment on column USER_MSG.sex

is ‘性別’;

comment on column USER_MSG.age

is ‘年齡’;

comment on column USER_MSG.address

is ‘家庭住址’;

comment on column USER_MSG.phone

is ‘電話’;

comment on column USER_MSG.status

is ‘狀態(是否失效)’;

comment on column USER_MSG.salt

is ‘加密加鹽’;

– Create/Recreate primary, unique and foreign key constraints

alter table USER_MSG

add primary key (ID)

using index

tablespace TS_MY_DATA

pctfree 10

initrans 2

maxtrans 255;

數據庫初始化完成

2.初始化maven項目

本人好久不用eclipse已經忘記怎麼使用eclipse 一直在用idea,感覺idea更智能一點,

仁者見仁智者見智。不做過多的評論。

(1).先創建maven項目

在這裏插入圖片描述
一個簡單的管理系統 Springmvc 一不留神就躺坑
需要注意這三個。

在這裏插入圖片描述
GroupID 是項目組織唯一的標識符,實際對應JAVA的包的結構,是main目錄裏java的目錄結構。

ArtifactID是項目的唯一的標識符,實際對應項目的名稱,就是項目根目錄的名稱。

groupId

定義了項目屬於哪個組,舉個例子,如果你的公司是mycom,有一個項目爲myapp,那麼groupId就應該是com.mycom.myapp.

artifacted

定義了當前maven項目在組中唯一的ID,比如,myapp-util,myapp-domain,myapp-web等。

version

指定了myapp項目的當前版本,SNAPSHOT意爲快照,說明該項目還處於開發中,是不穩定的版本。

一個簡單的管理系統 Springmvc 一不留神就躺坑
配置自己的maven倉庫位置勾選override可以選擇設置maven的settings.xml文件 系統會自動讀取settings.xml文件中的maven倉庫的配置地址

以後所有pom.xml文件引用的jar包全部會放入maven倉庫。

一個簡單的管理系統 Springmvc 一不留神就躺坑
創建maven項目存放的路徑。

(2).創建項目目錄結構

basics 存放基類 (一般不對外開放)

util 存放工具類(對外開放 包含basics中的方法)

controller 控制層

dao 數據訪問層

domain 實體層

Enum 枚舉層

service 業務層

一個簡單的管理系統 Springmvc 一不留神就躺坑
resources 存放配置文件

test 存放單元測試文件

webapp 存放頁面文件

一個簡單的管理系統 Springmvc 一不留神就躺坑
配置文件的位置及頁面存放的位置

(3).創建好目錄結構開始準備搭建環境

首先是引入項目所需要的jar包,寫在pom文件中

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<spring.version>3.2.4.RELEASE</spring.version>

<mybatis.version>3.2.4</mybatis.version>

<slf4j.version>1.6.6</slf4j.version>

<log4j.version>1.2.9</log4j.version>

org.apache.httpcomponents

httpclient

4.3.6

com.testingsyndicate

hc-jmx

2.1.0

jstl

jstl

1.2

junit

junit

4.9

test

commons-io

commons-io

2.4

commons-codec

commons-codec

1.9

org.apache.commons

commons-lang3

3.4

commons-fileupload

commons-fileupload

1.3.1

commons-dbcp

commons-dbcp

1.4

com.alibaba

fastjson

1.1.41

javax.servlet

jsp-api

2.0

provided

org.codehaus.castor

castor-xml

1.4.1

com.fasterxml.jackson.core

jackson-databind

2.9.4

org.aspectj

aspectjweaver

1.7.1

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-oxm

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-test

${spring.version}

org.springframework

spring-jdbc

${spring.version}

com.amazonaws

aws-java-sdk

1.11.221

org.mybatis

mybatis

${mybatis.version}

org.mybatis.generator

mybatis-generator-core

1.3.2

ojdbc5

ojdbc5

1.0.0

org.mybatis

mybatis-spring

1.2.2

log4j

log4j

${log4j.version}

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

org.springframework

spring-aspects

4.0.3.RELEASE

org.projectlombok

lombok-maven

1.16.20.0

pom

org.projectlombok

lombok

1.16.10

org.apache.poi

poi

3.17

pom文件內容

然後添加springmvc所需要的配置文件

web.xml配置文件

applicationContext.xml spring容器配置文件

<?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:tx=“http://www.springframework.org/schema/tx

xmlns:mvc=“http://www.springframework.org/schema/mvc

xmlns:aop=“http://www.springframework.org/schema/aop

xmlns:context=“http://www.springframework.org/schema/context

xsi:schemaLocation="http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-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/mvc

http://www.springframework.org/schema/mvc/spring-mvc-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/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">

<context:property-placeholder location=“classpath:config.properties”/>

classpath:mapper/*Mapper.xml

<context:component-scan base-package=“com.****” use-default-filters=“true”>

<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>

</context:component-scan>

<tx:annotation-driven transaction-manager=“tx”/>

<aop:aspectj-autoproxy proxy-target-class=“true”></aop:aspectj-autoproxy>

applicationContext.xml

這個文件的作用是配置數據庫連接的dbcp連接池 具體配置信息在config.properties文件中。

sqlSessionFactory:是mybatis中的一箇中要的對象,通俗講它是用來創建sqlSession對象的,而sqlSession用來操作數據庫的。

mapperLocations:裏面配置的是指明在dao層所映射的mapper文件的位置。

typeAliasesPackage:它一般對應我們的實體類所在的包,這個時候會自動取對應包中不包括包名的簡單類名作爲包括包名的別名。多個package之間可以用逗號或者分號等來進行分隔(value的值一定要是包的全)

MapperScannerConfigurer :自動掃描 將Mapper接口生成代理注入到Spring

存放數據訪問接口的位置

之前配置好的SqlSessionFactoryBean的name

註解掃描的要注意一個問題 use-default-filters=“true” 配置爲true註解掃描的時候就會排除@controller註解,如果配置爲false那麼如果該包內包含@controller則全部掃描<context:component-scan base-package=“com.***” use-default-filters=“true”>

<context:exclude-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>

</context:component-scan>

config.properties文件

1 jdbc.driver=oracle.jdbc.driver.OracleDriver
2 jdbc.url=jdbc:oracle:thin:@localhost:1521:ORCL
3 jdbc.username=****
4 jdbc.password=****
log4j配置文件

log4j.rootLogger=ERROR, stdout

SqlMap logging configuration…

log4j.logger.com.ibatis=DEBUG

log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG

log4j.logger.com.ibatis.sqlmap.engine.cache.CacheModel=DEBUG

log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientImpl=DEBUG

log4j.logger.com.ibatis.sqlmap.engine.builder.xml.SqlMapParser=DEBUG

log4j.logger.com.ibatis.common.util.StopWatch=DEBUG

log4j.logger.java.sql.Connection=DEBUG

log4j.logger.java.sql.Statement=DEBUG

log4j.logger.java.sql.PreparedStatement=DEBUG

log4j.logger.java.sql.ResultSet=DEBUG

Console output…

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

dispatcher-servlet.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-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd">

<context:component-scan base-package=“com.***” use-default-filters=“false”>

<context:include-filter type=“annotation” expression=“org.springframework.stereotype.Controller”/>

</context:component-scan>

mvc:annotation-driven</mvc:annotation-driven>

mvc:default-servlet-handler/

注意視圖解析器的配置

這個位置一定要寫全/WEB-INF/views/

初始化項目完成

這裏遇到了一個問題,通常情況下WEB-INF目錄是受服務器保護的,如果把vises文件夾直接放到wabapp下面則啓動項目可以直接訪問頁面

路徑:http://localhost:8080/***/views/login.jsp

但是把vises文件放到WEB-INF在訪問是會一直報404這時候如何解決問題?

訪問login.jsp則需要做一個controller入口

一個簡單的管理系統 Springmvc 一不留神就躺坑
路徑:http://localhost:8080/***/login/userlogin

這樣訪問controller根據返回值直接去找視圖解析器前後匹配,就會找到成功頁面。
寫在最後:

碼字不易看到最後了,那就點個關注唄,只收藏不點關注的都是在耍流氓!

關注並私信我“架構”,免費送一些Java架構資料,先到先得!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章