SSM(Spring+SpringMVC+MyBatis)框架入门
最近,开始SSM框架的学习,经过三四天与代码的厮杀后,终于学会了,在SSM框架中写 增删改查(数据库),今天controller写不下去了,就和大家先分享下经验,再与其相爱相杀吧!
对,(蓝瘦在这里,香菇),忘了说,
数据库是mysql,而且用了Navicat可视化工具
IDE是idea,听说是最好用的编译器
首先,教大家如何打开一个SSM项目
这里,framework就是我们要打开的项目,选择最外面的pom.xml(如图所示),来打开整个项目。
接着,展示一下目录
这个是大体上的目录,大概有.idea(自动生成),service,web三大目录
第一步,开始数据库的连接
在web目录下,按照左侧目录所示,一步步找到database-config.xml文件,点击打开,在
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/db_cartoon? ..../>
db_cartoon的位置上换上自己建立的数据库,其他的不用管,都是MyBatis里面的语句。
第二步,写实体类
在service目录下,打开Cartoon文件,把数据库里面有的字段都写出来。
package com.pandawork.common.entity;import com.pandawork.core.common.entity.AbstractEntity;
import com.sun.jmx.snmp.Timestamp;import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;import static com.sun.xml.internal.ws.policy.sourcemodel.wspolicy.XmlToken.Name;/*** Cartoon实体* Created by 阿鑫 on 2016/11/4.*/
@Table(name = "t_cartoon")
@Entity
public class Cartoon extends AbstractEntity { //为什么要继承AbstractEntity 呢?//动漫类别的序号@Idpublic Integer id;//这个得研究下//动漫类别的名称@Column(name = "name")private String name;//动漫的ID@Column(name = "p_id")private Integer pId;//动漫的名称@Column(name = "p_name")private String pName;//动漫的作者@Column(name = "author")private String author;//动漫的介绍@Column(name = "description")private String description;//创建时间@Column(name = "created_time")private Timestamp createdTime;//最后修改的时间public Timestamp getCreatedTime() {return createdTime;}public void setCreatedTime(Timestamp createdTime) {this.createdTime = createdTime;}public Timestamp getLastModifiedTime() {return lastModifiedTime;}public void setLastModifiedTime(Timestamp lastModifiedTime) {this.lastModifiedTime = lastModifiedTime;}@Column(name = "last_modified_time")private Timestamp lastModifiedTime;public Integer getId() {return id;}@Overridepublic void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Integer getpId() {return pId;}public void setpId(Integer pId) {this.pId = pId;}public String getpName() {return pName;}public void setpName(String pName) {this.pName = pName;}public String getAuthor() {return author;}public void setAuthor(String author) {this.author = author;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}@Overridepublic String toString() {return "Cartoon{" +"id=" + id +", name='" + name + '\'' +", pId=" + pId +", pName='" + pName + '\'' +", author='" + author + '\'' +", description='" + description + '\'' +'}';}}
第二步,开始写mapper文件和其对应的mapper.xml
在service目录下,打开CartoonMapper文件
package com.pandawork.mapper;import com.pandawork.common.entity.Cartoon;
import com.pandawork.common.entity.Student;
import com.pandawork.core.common.exception.SSException;
import org.apache.ibatis.annotations.Param;import java.util.List;/*** 动漫管理mapper层* Created by ${阿鑫} on 2016/11/4.*/
public interface CartoonMapper {/*** 增加动漫信息* @param cartoon* @throws Exception*/public void insert(@Param("cartoon") Cartoon cartoon) throws Exception;/*** 修改动漫信息* @param cartoon* @return* @throws Exception*/public void update(@Param("cartoon") Cartoon cartoon) throws Exception;/***删除动漫信息* @param id* @return* @throws Exception*/public boolean deleteById(@Param("id") int id) throws Exception;/*** 全部动漫信息条数* @return* @throws SSException*/public int countAll() throws Exception;/*** 查询动漫的数目* @param id* @return* @throws SSException*/public Cartoon queryById(@Param("id") int id) throws Exception;/*** 搜素Name中的字符* @param cartoonPName* @return* @throws SSException*/public Cartoon queryByPName(@Param("cartoonPName") String cartoonPName) throws Exception;}
对应的mapper.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="com.pandawork.mapper.UserMapper"><insert id="insert">INSERT INTO `t_user`(`username`,`password`)VALUES(#{user.username},#{user.password})</insert><delete id="delete">DELETE FROM `t_user`WHERE id = #{id}</delete><update id="update">UPDATE `t_user`SET `username` = #{user.username},`password` = #{user.password}WHERE id = #{user.id}</update><select id="queryById" resultMap="pw.User">SELECT id,username,passwordFROM `t_user`WHERE id = #{id}</select></mapper>
第三步,写service文件及其对应的实现serviceImpl
在service目录下,打开CartoonService文件
package com.pandawork.service;import com.pandawork.common.entity.Cartoon;
import com.pandawork.core.common.exception.SSException;/*** 动漫管理系统* CartoonService层* Created by ${阿鑫} on 2016/11/4.*/
public interface CartoonService {/*** 增加动漫信息* @param cartoon* @throws SSException*/public void insert(Cartoon cartoon) throws SSException;//SSException什么意思/*** 修改动漫信息* @param cartoon* @throws SSException*/public void update(Cartoon cartoon) throws SSException;/*** 删除动漫信息* @param id* @return* @throws SSException*/public boolean deleteById(int id) throws SSException;/*** 全部动漫信息条数* @return* @throws SSException*/public int countAll() throws SSException;/*** 根据id查询动漫的数目* @param id* @return* @throws SSException*/public Cartoon queryById(int id) throws SSException;/*** 搜素Name中的字符* @param cartoonPName* @return* @throws SSException*/public Cartoon queryByPName(String cartoonPName) throws SSException;
}
接着,打开对应的CartoonServiceImpl文件
package com.pandawork.service.impl;import com.pandawork.common.entity.Cartoon;
import com.pandawork.common.utils.NFException;
import com.pandawork.core.common.exception.SSException;
import com.pandawork.core.common.log.LogClerk;
import com.pandawork.core.common.util.Assert;
import com.pandawork.mapper.CartoonMapper;
import com.pandawork.service.CartoonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;/*** 动漫管理系统* CartoonService的实现* Created by ${阿鑫} on 2016/11/4.*/
@Service("cartoonService")//为啥要小写呀?
public class CartoonServiceImpl implements CartoonService {@AutowiredCartoonMapper cartoonMapper;public void insert(Cartoon cartoon) throws SSException {if (Assert.isNull(cartoon)) {return;}try {cartoonMapper.insert(cartoon);} catch (Exception e) {LogClerk.errLog.error(e);throw SSException.get(NFException.SystemException, e);}}@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {SSException.class, Exception.class, RuntimeException.class})//只要对数据库有改动,就得写public void update(Cartoon cartoon) throws SSException {if(Assert.isNull(cartoon)){return;}try {cartoonMapper.update(cartoon);}catch (Exception e) {LogClerk.errLog.error(e);throw SSException.get(NFException.SystemException, e);}}@Transactional(propagation = Propagation.REQUIRED, rollbackFor = {SSException.class, Exception.class, RuntimeException.class})public boolean deleteById(int id) throws SSException {if (Assert.lessOrEqualZero(id)) {return false;}try {return cartoonMapper.deleteById(id);} catch (Exception e) {LogClerk.errLog.error(e);throw SSException.get(NFException.DelStudentNull, e);//先不改了,NFException这个要改哦!DelStudentNull要不然这个。。}}public int countAll() throws SSException {int count;try {count = cartoonMapper.countAll();}catch (Exception e){LogClerk.errLog.error(e);throw SSException.get(NFException.CountAll, e);}return count;}public Cartoon queryById(int id) throws SSException{if(Assert.lessOrEqualZero(id)) {return null;}try {return cartoonMapper.queryById(id);} catch (Exception e) {LogClerk.errLog.error(e);throw SSException.get(NFException.queryStudentByIdFailed, e);}}public Cartoon queryByPName(String cartoonPName) throws SSException {if(Assert.isNull(cartoonPName)) {return null;}try {return cartoonMapper.queryByPName(cartoonPName);} catch (Exception e){LogClerk.errLog.error(e);throw SSException.get(NFException.QueryByNameFailed, e);}}
}
第四步,测试增删改查四个方法
注意,这里test文件夹是绿色的,这是因为把这个test文件夹设置成了test sources root(设置方法如下图)
在web文件夹中,打开CartoonServiceTest文件。
package com.pandawork.test;import com.pandawork.common.entity.Cartoon;
import com.pandawork.core.common.exception.SSException;
import com.pandawork.service.CartoonService;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;/*** 动漫管理系统* 测试service页面* CartoonService* Created by ${阿鑫} on 2016/11/4.*/
public class CartoonServiceTest extends AbstractTestCase{@AutowiredCartoonService cartoonService;//测试新增动漫@Testpublic void testInsert() throws SSException{Cartoon cartoon = new Cartoon();cartoon.setName("热血动漫");cartoon.setpId(999);cartoon.setpName("bigbang");cartoon.setAuthor("啦啦啦啦");cartoon.setDescription("哈哈");cartoonService.insert(cartoon);}//测试修改动漫@Testpublic void testUpdate() throws SSException{Cartoon cartoon = new Cartoon();cartoon.setName("教育动漫");cartoon.setpId(666);cartoon.setpName("大耳朵图图");cartoon.setAuthor("周月");cartoon.setDescription("动耳神功");cartoon.setId(1);cartoonService.update(cartoon);System.out.println(cartoon);}//测试删除动漫@Testpublic void testDelete() throws SSException{cartoonService.deleteById(8);}//测试全部动漫信息条数@Testpublic void testCountAll() throws SSException{System.out.println(cartoonService.countAll());}//测试根据ID查询动漫信息@Testpublic void testQueryById() throws SSException {System.out.println(cartoonService.queryById(3));}//测试根据名称查询动漫信息@Testpublic void testQueryByPName() throws SSException{System.out.println(cartoonService.queryByPName("大"));}
}
这个就是大概的流程和代码了!我试过了,几种方法都是可以跑通的,大家可以放心借鉴哈,在稍后的系列里会给大家讲述一些更加详细的东西。
谢谢观看!
SSM(Spring+SpringMVC+MyBatis)框架入门相关推荐
- SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)
SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...
- Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据、人工智能、机器学习项目开发
Java语言开发在线音乐推荐网 音乐推荐系统 网易云音乐爬虫 基于用户.物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)框架 大数据.人工智能.机器学习项目开发Mus ...
- Java语言开发在线美食推荐网 美食推荐系统 基于用户、物品的协同过滤推荐算法实现 SSM(Spring+SpringMVC+Mybatis框架 人工智能、大数据、机器学习项目开发
Java语言开发在线美食推荐网 美食推荐系统 基于用户.物品的协同过滤推荐算法实现 SSM(Spring+SpringMVC+Mybatis框架 人工智能.大数据.机器学习项目开发FoodRecomm ...
- Spring+SpringMVC+Mybatis框架集成搭建教程
一.背景 最近有很多同学由于没有过SSM(Spring+SpringMvc+Mybatis , 以下简称SSM)框架的搭建的经历,所以在自己搭建SSM框架集成的时候,出现了这样或者那样的问题,很是苦恼 ...
- Java语言开发在线购物推荐网 购物商城推荐系统 基于用户、物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据、人工智能、机器学习项目开发
Java语言开发在线购物推荐网 购物商城推荐系统 基于用户.物品的协同过滤推荐算法 SSM(Spring+SpringMVC+Mybatis)开发框架 大数据.人工智能.机器学习项目开发ShopRec ...
- Java+SSM(Spring+SpringMVC+Mybatis)个性化购物商城推荐系统 电子商务推荐系统 基于用户、项目、聚类、混合的协同过滤推荐算法WebShopRSMEx 源代码下载
Java+SSM(Spring+SpringMVC+Mybatis)个性化购物商城推荐系统 电子商务推荐系统 基于用户.项目.聚类.混合的协同过滤推荐算法WebShopRSMEx 源代码下载 一.项目 ...
- 使用Java+SSM(Spring+SpringMVC+Mybatis)如何开发个性化音乐推荐系统 在线音乐推荐网站 基于用户、项目的协同过滤推荐算法实现MusicRecommendSystemWeb
使用Java+SSM(Spring+SpringMVC+Mybatis)如何开发个性化音乐推荐系统 在线音乐推荐网站 基于用户.项目的协同过滤推荐算法实现MusicRecommendSystemWeb ...
- 如何使用Java+SSM(Spring+SpringMVC+Mybatis)开发个性化新闻推荐系统 在线新闻推荐系统 基于用户项目协同过滤、内容、聚类、关联规则推荐算法实现WebNewsRSMEx
如何使用Java+SSM(Spring+SpringMVC+Mybatis)开发个性化新闻推荐系统 在线新闻推荐系统 基于用户项目协同过滤.内容.聚类.关联规则推荐算法实现WebNewsRSMEx 一 ...
- 使用Java+SSM(Spring+SpringMVC+Mybatis)开发在线美食推荐网 美食推荐系统 美食天下美食爬虫 基于用户、物品的协同过滤推荐算法实现 大数据、人工智能、机器学习项目开发
使用Java+SSM(Spring+SpringMVC+Mybatis)开发在线美食推荐网 美食推荐系统 美食天下美食爬虫 基于用户.物品的协同过滤推荐算法实现 大数据.人工智能.机器学习项目开发Fo ...
- Springboot+ssm(Spring+SpringMVC+MyBatis)旧物置换网站
目 录 摘 要 I 目 录 III 第1章 概述 1 1.1 研究背景 1 1.2 研究现状 1 1.3 研究内容 2 第二章 开发技术介绍 2 2.1 ...
最新文章
- MonoRail学习笔记五:定制服务实现自定义功能
- 你会不会用mysql查询近7个月的数据?没有记录默认为空
- ThreeJS的特效合成器和后期处理通道
- Pandas CookBook -- 02DataFrame基础操作
- android 如何使用SAX解析XML
- 推荐系统-基于矩阵分解的LFM模型
- java代码_35个Java 代码优化细节
- html特效指令,vue2——指令v-text v-html v-bind
- 计算机考试换机密码,Ami换机,让你轻松转移手机资料!
- 如何将c语言中的文件,急求如何将下列C语言程序数据存储到文件中?
- python基础--函数1
- 11 个创新的网站滑动效果设计案例展示
- 串行通信接口详细描述
- 小程序页面调用服务器接口授权,小程序的授权和登陆
- 使用Windows批处理压缩文件
- android手机蓝牙连接扫码枪,android 扫码枪解惑
- 台式计算机拆卸步骤,拆装台式电脑主机的方法图解步骤
- 文件系统以及硬盘分区概念
- 【阿里内推一面】记我人生中的处女面
- Java Word转PDF
热门文章
- word中套用表格样式在哪里_表格套用表格样式在哪 word表格自动套用样式在哪
- 形象思维图谱应用--树形图
- RecordCount 属性
- linux下c 开发 腾讯视频播放器,调用腾讯视频播放器API代码
- MATLAB编写m函数理解 y=f(g(x))*h(x)
- WPF QQ群发助手
- linux系统电视u盘安装教程,linux系统U盘安装教程
- 自动化运维工具puppet学习笔记之基础篇
- Failed to open the host Key database file
- pe服务器注册表,在 win pe 下修改本机系统注册表