最近,开始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 呢?
//动漫类别的序号
@Id
public 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;
}
@Override
public 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;
}
@Override
public 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,password
FROM `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 {
@Autowired
CartoonMapper 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{
@Autowired
CartoonService cartoonService;
//测试新增动漫
@Test
public void testInsert() throws SSException{
Cartoon cartoon = new Cartoon();
cartoon.setName("热血动漫");
cartoon.setpId(999);
cartoon.setpName("bigbang");
cartoon.setAuthor("啦啦啦啦");
cartoon.setDescription("哈哈");
cartoonService.insert(cartoon);
}
//测试修改动漫
@Test
public 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);
}
//测试删除动漫
@Test
public void testDelete() throws SSException{
cartoonService.deleteById(8);
}
//测试全部动漫信息条数
@Test
public void testCountAll() throws SSException{
System.out.println(cartoonService.countAll());
}
//测试根据ID查询动漫信息
@Test
public void testQueryById() throws SSException {
System.out.println(cartoonService.queryById(3));
}
//测试根据名称查询动漫信息
@Test
public void testQueryByPName() throws SSException{
System.out.println(cartoonService.queryByPName("大"));
}
}
这个就是大概的流程和代码了!我试过了,几种方法都是可以跑通的,大家可以放心借鉴哈,在稍后的系列里会给大家讲述一些更加详细的东西。
谢谢观看!