基于SSM的CRUD项目准备工作

基于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、创建数据表
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,我们想让每次查询都能直接得到对应的部门信息,所以需要对自动生成的类进行一些修改,步骤如下:

  • Student类中添加Department属性
  • 原mapper接口中有方法

    ```java
    List<Student> selectByExample(StudentExample example);
    Student selectByPrimaryKey(Integer stuId);
    ```
    

​ 我们再添加两个:

 List<Student> selectByExampleWithDept(StudentExample example);
Student selectByPrimaryKeyWithDept(Integer stuId);
  • StudentMapper.xml中进行修改,其实都是复制原有自动生成的,再进行稍微修改实现分步查询。
  <!--带上部门后的resultMap-->
  <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" >
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LeoCache

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值