基于SSM的CRUD项目准备工作
该项目是基于SSM的增删改查项目,前端淘汰了过时的jsp技术,通过Bootstrap+vue+Thymeleaf实现。完整项目下载链接
一、SSM文件配置
1、web.xml
- 编码过滤器和处理请求方式过滤器
- DispatcherServlet前端控制器(要引入SpringMVC.xml配置文件路径)
- 引入Spring配置文件路径
- Spring监听器ContextConfigLocation
2、SpringMVC.xml
- 扫描控制层组件
- 视图解析器Thymeleaf
- 配置默认Servlet处理静态资源
- 开启MVC注解驱动
- 配置视图控制器
3、Spring.xml
- 扫描除控制层以外组件
- 配置数据源(先引用数据库配置文件)
- 配置SqlSessionFactoryBean
- 配置mapper接口的扫描
二、创建数据表使用逆向工程
1、创建数据表
1 2 3 4 5 6 7 8 9 10 11 12 13
| CREATE TABLE `tbl_stu` ( `stu_id` int NOT NULL AUTO_INCREMENT, `stu_name` varchar(2295) DEFAULT NULL, `gender` char(9) DEFAULT NULL, `email` varchar(2295) DEFAULT NULL, `d_id` int DEFAULT NULL, PRIMARY KEY (`stu_id`) ) CREATE TABLE `tbl_dept` ( `dept_id` int NOT NULL AUTO_INCREMENT, `dept_name` varchar(765) DEFAULT NULL, PRIMARY KEY (`dept_id`) )
|
2、完成逆向工程,更改生成的mapper
先完成generatorConfig.xml文件的配置,再使用逆向工程插件生成数据表对应的Pojo、Mapper和sql接口映射文件。因为自动生成的查询学生只能得到他所在的部门id,我们想让每次查询都能直接得到对应的部门信息,所以需要对自动生成的类进行一些修改,步骤如下:
我们再添加两个:
1 2
| List<Student> selectByExampleWithDept(StudentExample example); Student selectByPrimaryKeyWithDept(Integer stuId);
|
- StudentMapper.xml中进行修改,其实都是复制原有自动生成的,再进行稍微修改实现分步查询。
1 2 3 4 5 6 7 8 9 10 11 12 13
| <resultMap id="BaseResultMapWithDept" type="com.guoliang.ssm.pojo.Student" > <id column="stu_id" property="stuId" jdbcType="INTEGER" /> <result column="stu_name" property="stuName" jdbcType="VARCHAR" /> <result column="gender" property="gender" jdbcType="CHAR" /> <result column="email" property="email" jdbcType="VARCHAR" /> <result column="d_id" property="dId" jdbcType="INTEGER" /> <association property="department" select="com.guoliang.ssm.mapper.DepartmentMapper.selectByPrimaryKey" column="d_id"></association> </resultMap> <select id="selectByExampleWithDept" resultMap="BaseResultMapWithDept" parameterType="com.guoliang.ssm.pojo.StudentExample" > <select id="selectByPrimaryKeyWithDept" resultMap="BaseResultMapWithDept" parameterType="java.lang.Integer" >
|