一、简介
(1)CRUD是什么
- CRUD:
Create
( 创 建 )Retrieve
(查询)Update
(更新)Delete
(删除) - 一个项目最基础也是最常用到的就是增删改查
(2)数据库准备
create database ssm;
use ssm;
create table department(
did int primary key auto_increment,
dname varchar(20)
)
insert into department values(null,'java');
insert into department values(null,'测试');
insert into department values(null,'需求');
create table employee(
eid int primary key auto_increment,
ename varchar(20),
gender varchar(20),
did int
)
insert into employee values(null,'jack','1',1);
insert into employee values(null,'rose','1',1);
insert into employee values(null,'tony','1',2);
(3)ssm搭建
(1)请参考:框架的整合流程【框架源代码】
(2)复制框架代码ssm为ssm02_crud,并刷新
- 修改pom.xml(ssm02_crud)
<artifactId>ssm02_crud</artifactId>
<packaging>war</packaging>
<name>ssm02_crud</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
- pom.xml(ssm)
<modules>
<module>ssm01</module>
<module>ssm02</module>
<module>ssm02_crud</module>
</modules>
(4)实体类代码
- Department.java(src\main\java\domain)
public class Department {
private Integer did;
private String dname;
public Department() {
}
public Department(String dname) {
this.dname = dname;
}
@Override
public String toString() {
return "Department{" +
"did=" + did +
", dname='" + dname + '\'' +
'}';
}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
}
二、查询
(1)后台代码
TestDepartmentService
(src\test\java\service\TestDepartmentService.java)
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDepartmentService {
private static final Logger l= LoggerFactory.getLogger(TestDepartmentService.class);
@Autowired
IDepartmentService service;