项目环境背景:
操作系统:win7
JDK:1.7
相关依赖包,截图如下:
项目目录截图:
com.sgl.controller:存放Controller相关类
在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层处理之后封装成一个Model ,然后再把该Model 返回给对应的View 进行展示。在SpringMVC 中提供了一个非常简便的定义Controller 的方法,你无需继承特定的类或实现特定的接口,只需使用@Controller 标记一个类是Controller ,然后使用@RequestMapping 和@RequestParam 等一些注解用以定义URL 请求和Controller 方法之间的映射,这样的Controller 就能被外界访问到。此外Controller 不会直接依赖于HttpServletRequest 和HttpServletResponse 等HttpServlet 对象,它们可以通过Controller 的方法参数灵活的获取到.
com.sgl.model:存放与数据库对应的对象模型
com.sgl.dao:放一些处理model类的接口类和接口实现类
com.sgl.mapper:除了SqlMapper之外,其他的mapper与对应的DAO是一样的,都是一些接口。SqlMapper是其他mapper的父类。
com.sgl.mapping:存放对应model的所有数据库操作
com.sgl.service:业务逻辑处理
下面是所有用到的配置文件介绍:
第一个是spring 的配置文件applicationContext-common.xml
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
com.mysql.jdbc.Driver
jdbc:mysql://localhost:3306/electDB
root
123
classpath:com/sgl/mapping/*.xml
View Code
第二个是mybatis的配置文件,由于spring 已经将其数据库相关连接配置管理了,这mybatis中需要做的事情比减少。我这里基本没有做什么处理。只是映射一下model的别名。
View Code
第三个是springmvc的配置。
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
/WEB-INF/pages/
.jsp
第四个是mybatis的映射配置文件
/p>
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
select * from electuser
insert into
electuser(id,username,
password,realname,email,cellphone,sex,birthday,regdate,picpath,roleid,statue)
values(#{id}, #{username},
#{password},#{realname},#{email},#{cellphone},#{sex},#{birthday},#{regdate},,#{picpath},#{roleid},#{statue})
最后一个是web.xml配置文件。作为网站的入口,在这里需要将以上的一些配置文件进行加载,管理。网站的初始化等工作
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
index.jsp
org.springframework.web.context.ContextLoaderListener
contextConfigLocation
classpath:applicationContext-*.xml
dispatcher
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
/WEB-INF/dispatcher.xml
1
dispatcher
*.do
characterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
characterEncodingFilter
/*
接下来将里面的一些类、接口做一些介绍。
model里面的就不做说明了,就是简单的实体类,与数据库对应字段就OK。
dao:
这里的accountMapper与applicationContext-common.xml里面 的对应
com.sgl.mapper:
sqlmapper是所有mapper接口的父类接口,在这里说明一下,SqlMapper里面可以定义一些模板方法,也可以不定义,什么都不写。只是作为一个父类接口做逻辑上的关系。
com.sgl.service:
Controller:
至此所有的代码完毕:在浏览器输入:http://localhost:端口/项目名称/Info/list.do,进行查看你的结果就行了。