一个简单的管理系统 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架构资料,先到先得!

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