步骤一:导包:1.Spring-context包 含有 核心容器中的四个jar包和aop包…大致32个jar包
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.5.4version>
dependency>
<dependency>
<groupId>aopalliancegroupId>
<artifactId>aopallianceartifactId>
<version>1.0version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-txartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>commons-beanutilsgroupId>
<artifactId>commons-beanutilsartifactId>
<version>1.7.0version>
dependency>
<dependency>
<groupId>commons-dbutilsgroupId>
<artifactId>commons-dbutilsartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-aspectsartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>5.0.5.RELEASEversion>
dependency>
<dependency>
<groupId>commons-logginggroupId>
<artifactId>commons-loggingartifactId>
<version>1.1.1version>
dependency>
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.12version>
dependency>
<dependency>
<groupId>commons-iogroupId>
<artifactId>commons-ioartifactId>
<version>2.5version>
dependency>
<dependency>
<groupId>commons-fileuploadgroupId>
<artifactId>commons-fileuploadartifactId>
<version>1.4version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
<dependency>
<groupId>javax.servletgroupId>
<artifactId>javax.servlet-apiartifactId>
<version>3.0.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>javax.servlet.jspgroupId>
<artifactId>javax.servlet.jsp-apiartifactId>
<version>2.2.1version>
<scope>providedscope>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.6version>
<scope>runtimescope>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.2.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.2version>
dependency>
<dependency>
<groupId>cglibgroupId>
<artifactId>cglibartifactId>
<version>2.2.2version>
dependency>
dependencies>
步骤二:在resources资源文件夹下面新建两个目录。一个是spring(Spring的核心配置),一个是mybatis(核心配置)
步骤三:配置db.properties(数据库连接信息)和log4j.properties(日志配置文件)在resources目录下
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///db_springmvc?CharacterEncoding=UTF-8
username=root
password=admin
log4j.properties
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# 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
步骤四:在spring目录下创建springmvc.xml文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
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">
<mvc:annotation-driven/>
<context:component-scan base-package="com.zhiyou100.web.Controller"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view"/>
<property name="suffix" value=".jsp"/>
bean>
beans>
步骤五:在spring目录下创建applicationContext-dao.xml
<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"
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">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
bean>
<bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/>
<property name="typeAliasesPackage" value="com.zhiyou100.pojo"/>
bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zhiyou100.mapper"/>
bean>
beans>
步骤五:在spring目录下创建applicationContext-service.xml文件
<coontext:component-scan base-package="com.zhiyou100.service"/>
步骤六:在spring目录下创建applicationContext-tx.xml文件(配置事务)
<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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
bean>
<tx:advice transaction-manager="transactionManager" id="txAdvice">
<tx:attributes>
<tx:method name="find*" propagation="REQUIRED" read-only="true" isolation="DEFAULT" timeout="-1"/>
<tx:method name="get*" propagation="REQUIRED" read-only="true" isolation="DEFAULT" timeout="-1"/>
<tx:method name="select*" propagation="REQUIRED" read-only="true" isolation="DEFAULT" timeout="-1"/>
<tx:method name="query*" propagation="REQUIRED" read-only="true" isolation="DEFAULT" timeout="-1"/>
<tx:method name="add*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
<tx:method name="insert*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
<tx:method name="append*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
<tx:method name="delete*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
<tx:method name="remove*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
<tx:method name="update*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
<tx:method name="modify*" propagation="REQUIRED" read-only="false" isolation="DEFAULT" timeout="-1"/>
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="pct" expression="execution(* com.zhiyou100.service..*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pct"/>
aop:config>
beans>
步骤七:在mybatis目录下创建mybatis-config.xml文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis主配置文件-->
<configuration>
</configuration>
步骤八:配置web.xml文件
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/applicationContext-*.xmlparam-value>
context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<servlet>
<servlet-name>DispatcherServletservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/springmvc.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>DispatcherServletservlet-name>
<url-pattern>*.actionurl-pattern>
servlet-mapping>
web-app>
步骤九:创建mapper、util、pojo包以及对应的类
pojo实体类
public class Role implements Serializable {
/**
* 按照数据库中的字段匹配
*/
private Integer r_id;
private String r_name;
private String r_desc;
private String r_rTime;
public Role() {
}
public Role(Integer r_id, String r_name, String r_desc, String r_rTime) {
this.r_id = r_id;
this.r_name = r_name;
this.r_desc = r_desc;
this.r_rTime = r_rTime;
}
public Integer getR_id() {
return r_id;
}
public void setR_id(Integer r_id) {
this.r_id = r_id;
}
public String getR_name() {
return r_name;
}
public void setR_name(String r_name) {
this.r_name = r_name;
}
public String getR_desc() {
return r_desc;
}
public void setR_desc(String r_desc) {
this.r_desc = r_desc;
}
public String getR_rTime() {
return r_rTime;
}
public void setR_rTime(String r_rTime) {
this.r_rTime = r_rTime;
}
@Override
public String toString() {
return "Role{" +
"r_id=" + r_id +
", r_name='" + r_name + '\'' +
", r_desc='" + r_desc + '\'' +
", r_rTime='" + r_rTime + '\'' +
'}';
}
}
mapper映射接口以及mapper映射文件:
//RoleMapper接口
public interface RoleMapper {
//查询所有的角色信息
List<Role> getAllRoles();
}
<mapper namespace="com.zhiyou100.mapper.RoleMapper">
<select id="getAllRoles" resultType="role">
select * from t_role
select>
mapper>
业务层接口及其实现类(service层)
public interface RoleService {
/**
* 获取所有的角色信息
* @return
*/
List<Role> getAllRoles();
}
@Service("roleService")
public class RoleServiceImpl implements RoleService {
//属性注入
@Autowired
private RoleMapper roleMapper;
@Override
public List<Role> getAllRoles() {
return roleMapper.getAllRoles();
}
}
控制层编写功能实现(controller层)
方式一:
/**
* @Controller
* 把当前的类交给spring管理
*/
@Controller("roleController")
public class RoleController {
//属性注入
@Autowired
private RoleService roleService;
//构建获取所有的角色信息的服务
@RequestMapping("/role/listRoles.action")
public ModelAndView getAllRoles(){
//构建modelAndView模型视图组件
ModelAndView mav = new ModelAndView();
//获取service层中的结果值
List<Role> allRoles = roleService.getAllRoles();
//把roles存进model模型组件中
mav.addObject("list", allRoles);
//指定视图地址
mav.setViewName("/role/roleList");
return mav;
}
}
方式二:
@RequestMapping("/role/listRoles.action")
public void getAllRoles02(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
//获取service层中的结果值
List<Role> allRoles = roleService.getAllRoles();
//把roles存进request域中
request.setAttribute("list", allRoles);
//内部资源转发
request.getRequestDispatcher("/role/roleList").forward(request, response);
}
方式三
@RequestMapping("/role/listRoles.action")
public String getAllRoles03(Model model){
//获取service层中的结果值
List<Role> allRoles = roleService.getAllRoles();
model.addAttribute("list", allRoles);
return "/role/roleList";
}