您的位置:首页 > 其它

基于SSM框架的增删改查

2020-01-11 08:16 246 查看

源码地址: https://github.com/zhangxycg/ssm_crud

如要查看源码,请点击我

视频地址: https://www.bilibili.com/video/av21045215

尚硅谷Java视频教程

文章目录

  • 六、 踩坑
  • 一、使用的技术

    基础框架: ssm(SpringMVC + Spring + MyBatis)
    前端框架: Bootstrap
    数据库: MySQL
    项目的依赖管理: Maven
    分页: pagehelper
    逆向工程: MyBatis Generator

    二、环境的搭建

    1. 创建一个maven工程

    2. 在pom文件中引入项目依赖的jar包
      • spring
      • springmvc
      • mybatis
      • 数据库连接池(c3p0)
      • mysql驱动包
      • 其他(jstl,servlet-api,junit)

    3. 引入bootstrap前端框架

    4. 编写ssm整合的关键配置文件
      web.xml,spring,springmvc,mybatis,使用mybatis的逆向工程生成对应的 pojo 以及 mapper

    5. 测试mapper

    三、页面效果图

    四、项目结构图

    五、功能及其实现逻辑

    1. 查询功能

    1. 在浏览器访问 index.jsp 页面
    2. index.jsp 页面发出查询员工列表的请求
    3. 后台 EmployeeController 接收前台发过来的请求,查出员工数据
    4. 来到 list.jsp 页面进行展示查询出的数据
    5. pageHelper 分页插件完成分页查询功能

    2. 新增功能

    1. 在 index.jsp 页面点击 “ 新增 ” 按钮
    2. 弹出员工新增的对话框
    3. 然后去数据库查询部门列表,显示在对话框中
    4. 用户输入数据,并进行校验
    5. jquery前端校验,ajax用户名重复校验,重要数据(后端校验使用了JSR303);
    6. 数据填写正确,点击保存按钮

    3. 修改功能

    1. 在页面点击 “编辑” 按钮
    2. 弹出用户修改的模态框
    3. 修改数据(用户名无法修改)
    4. 修改完成,点击 “更新 ” 按钮,完成用户修改

    4. 删除功能

    1. 单个删除
      点击删除按钮,弹出提示信息,点击确定。将会删除数据
    2. 批量删除
      勾选员工姓名前的选择框,可以实现批量删除

    六、 踩坑

    第一次踩坑: 连接不上数据库 ,报错信息如下

    java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:129)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:89)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:63)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:73)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:76)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
    at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:455)
    at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
    at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
    at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:175)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:220)
    at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:206)
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1PooledConnectionResourcePoolManager.acquireResource(C3P0PooledConnectionPool.java:203)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquire(BasicResourcePool.java:1138)
    at com.mchange.v2.resourcepool.BasicResourcePool.doAcquireAndDecrementPendingAcquiresWithinLockOnSuccess(BasicResourcePool.java:1125)
    at com.mchange.v2.resourcepool.BasicResourcePool.access$700(BasicResourcePool.java:44)
    at com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask.run(BasicResourcePool.java:1870)
    at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:696)
    Caused by: com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
    at sun.reflect.GeneratedConstructorAccessor45.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
    at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:85)
    at com.mysql.cj.util.TimeUtil.getCanonicalTimezone(TimeUtil.java:132)
    at com.mysql.cj.protocol.a.NativeProtocol.configureTimezone(NativeProtocol.java:2243)
    at com.mysql.cj.protocol.a.NativeProtocol.initServerSession(NativeProtocol.java:2267)
    at com.mysql.cj.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:1319)
    at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:966)
    at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
    ... 12 more


    报错原因是

    在使用mysql的jdbc驱动最新版(6.0+)版本时,数据库和系统时区差异引起的问题。

    解决办法:

    第一种是 降低驱动的版本

    第二种是:在jdbc连接的url后面加上 ?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&characterEncoding=UTF8

    第二次踩坑: 字新增与修改完成以后,员工数据不按 id 进行升序排列

    解决办法:

    修改 EmployeeMapper.xml 中的查询语句,使员工数据按 员工 id 进行升序排列

    <select id="selectByExampleWithDept" resultMap="WithDeptResultMap">
    select
    <if test="distinct">
    distinct
    </if>
    <include refid="WithDept_Column_List" />
    FROM tbl_emp e
    left join tbl_dept d on e.`d_id`=d.`dept_id`
    <if test="_parameter != null">
    <include refid="Example_Where_Clause" />
    </if>
    <!-- 员工数据根据id进行升序排列 -->
    order by e.`emp_id`  asc
    </select>
    • 点赞 19
    • 收藏
    • 分享
    • 文章举报
    扬帆向海 发布了73 篇原创文章 · 获赞 818 · 访问量 6万+ 私信 关注
    内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
    标签: