在学习SpringMVC技术的时候,配置是最多的了,接下来带大家一步一步的创建一个SprignMVC环境,然后测试一个小案例,后续大家跟着这个写代码就行了
1、数据库准备
准备一个数据库,随便一个表都行,搭建环境只需要连接到就行,我这里有个user表
2、准备idea创建web项目
①先新建一个普通的java项目
②右键添加模块
③选择web appliaction【web应用程序】
④就可以看到创建了一个webjava的项目了,里面多了一个web包,里面也有WEB-INF这些目录
3、接下来准备搭建SSM【Spring+Mybatis+SpringMVC】环境
注意:因为SpringMVC是基于Spring的,所以我们直接搭建好SpringMVC就可以拥有了Spring的环境
①在WEB-INF下面创建一个lib目录,然后导入SSM会用到的相关jar包
②因为这个时候还没法使用这些包,需要我们添加到库,点击lib右键点击add as Library
③接下来配置SpringMVC中最重要的环节,在web.xml中配置前端控制器的信息
<?xml version="1.0" encoding="UTF-8"?>
<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">
<!--前端控制器配置-->
<servlet>
<servlet-name>ssm</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<!--加载级别,初始化加载,值越小优先级越高-->
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ssm</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
④因为SpringMVC和Spring的衔接就是在前端控制器里面配置Spring的核心配置文件,所以这个时候需要我们准备上Spring的核心配置文件applicationContext.xml,按照图中的顺序点击添加,就可以有spring的一些约束
⑤这里面的约束只够spring使用,还需要添加web,mvc这些模块用的约束,所以我们把约束添加上
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
</beans>
⑥spring的核心配置文件配置好了后,SpringMVC就好了,但是Spring本身的一些信息,例如包扫描,还有Mybatis的信息还没有准备好,这个时候我们来创建一些包结构,因为后面写代码都需分层
⑦这个时候就可以配置包扫描啦,在很多包中都会有bean存在,所以我们扫描的位置要大,在applicationContext.xml文件中添加包扫描
<context:component-scan base-package="cn.xjxynxb.*"></context:component-scan>
⑧这个时候spring的基本就配置好了,接下来开始配置mybatis的信息,mybatis主要是需要操作数据库,那么我们需要在src下面准备一个jdbc.properties的数据库信息文件
⑨我们在applicationContext.xml文件中需要配置mybatis,将其和spring进行一个集成,得益于之前我们导入的包中有一个叫mybatis-spring,这里面有一个核心类叫SqlSessionFactoryBean【学过mybatis都知道,最核心的类叫SqlSession对吧,在这里的这个核心类就相当于是配置SqlSession的这些信息】,因为mybatis需要用到一些步骤,我直接把配置文件发出来大家使用就行了
<!--2、配置数据源-->
<!--①引入jdbc-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--②配置druid数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--③配置mybatis相关-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源参数-->
<property name="dataSource" ref="druidDataSource"></property>
<!--配置mapper-->
<property name="mapperLocations" value="classpath:cn/wolfcode/mapper/UsersMapper.xml"/>
</bean>
⑩这个时候大家会发现这个里面的有个参数叫mapperLocations,这里面是mapper包下面关于dao层接口的sql文件的一个路径,所以这里需要大家去在mapper文件下新建一个叫UsersMapper.xml,然后把约束放到文件中,这个路径就不会爆红了
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.wolfcode.dao.UsersDao">
</mapper>
⑩①这个时候mybatis连接数据库的差不多了,但是还少一个对于dao层接口的配置,因为mybatis底层通过动态代理,这里还没有关于接口和sql文件的关联关系,所以需要配置一个dao层接口扫描,在项目启动的时候就去查找到
<!--3、配置一个接口扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置mybatis相关的bean名字-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="cn.wolfcode.dao"></property>
</bean>
⑩②综上,我们的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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<!--1、包扫描-->
<context:component-scan base-package="cn.wolfcode.*"/>
<!--2、配置数据源-->
<!--①引入jdbc-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--②配置druid数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--③配置mybatis相关-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--配置数据源参数-->
<property name="dataSource" ref="druidDataSource"></property>
<!--配置mapper-->
<property name="mapperLocations" value="classpath:cn/wolfcode/mapper/UsersMapper.xml"/>
</bean>
<!--3、配置一个接口扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--配置mybatis相关的bean名字-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="cn.wolfcode.dao"></property>
</bean>
</beans>
4、配置设置好了后,我们测试一个查询数据库有多少条数据的案例进行测试
①dao层接口
public interface UsersDao {
/*查询数据库中有多少条数据*/
public Integer selectCount();
}
②mapper.xml中的sql语句
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.wolfcode.dao.UsersDao">
<select id="selectCount" resultType="integer">
select count(1) from t_users
</select>
</mapper>
③service层接口和实现类
public interface UsersService {
//获取总数
Integer getCount();
}
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersDao usersDao;
@Override
public Integer getCount() {
return usersDao.selectCount();
}
}
④Controller接口
@Controller
public class UsersController {
@Autowired
private UsersService usersService;
@RequestMapping("/getCount")
public ModelAndView getCount(){
//查询总数数据
Integer i=usersService.getCount();
ModelAndView view = new ModelAndView();
view.setViewName("index.jsp");
view.addObject("count",i);
return view;
}
}
⑤这个时候我们的后端代码没问题了,但是希望有一个前端页面能够展示,所以就在index.jsp页面进行一个改造
<%--
Created by IntelliJ IDEA.
User: d
Date: 2022/10/23
Time: 22:13
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
哈喽,数据一共有${count}条
</body>
</html>
⑥经过一番操作,我们的代码就写完了,结构如下
⑦这个时候我们打开tomcat进行测试,如果有的铁子不知道tomcat怎么设置,请看我的另一篇文章 写文章-CSDN创作中心
⑧测试访问如下,就ok啦,我数据库只有一条数据