ssm框架下对数据库表的增查删改

在ssm框架下对数据库表单的增查删改操作(分页查询)

一.编写流程

配置ssm整合——创建Java实体类——创建服务方法service接口——创建方法与参数的映射关系Mapper接口——创建实体类方法实现类Impl实现service接口中的方法——设置方法与数据库逻辑的Mapper.xml配置——编写逻辑控制层control类

二样例 

建立公司员工库,并对员工库进行增查删改以及分页操作

1创建员工实体类、分页page类

员工类

[java]  view plain  copy
  1. public class Employee {  
  2. /** 
  3.  * 员工测试类 
  4.  */  
  5. private int id;         //编号  
  6. private String name;    //姓名  
  7. private String email;   //邮箱  
  8. private String phone;   //电话  
  9. private Date add_date;  //创建日期  
  10. private String position;    //职位  
  11. public int getId() {  
  12.     return id;  
  13. }  
  14. public void setId(int id) {  
  15.     this.id = id;  
  16. }  
  17. public String getName() {  
  18.     return name;  
  19. }  
  20. public void setName(String name) {  
  21.     this.name = name;  
  22. }  
  23. public String getEmail() {  
  24.     return email;  
  25. }  
  26. public void setEmail(String email) {  
  27.     this.email = email;  
  28. }  
  29. public String getPhone() {  
  30.     return phone;  
  31. }  
  32. public void setPhone(String phone) {  
  33.     this.phone = phone;  
  34. }  
  35. public Date getAdd_date() {  
  36.     return add_date;  
  37. }  
  38. public void setAdd_date(Date add_date) {  
  39.     this.add_date = add_date;  
  40. }  
  41. public String getPosition() {  
  42.     return position;  
  43. }  
  44. public void setPosition(String position) {  
  45.     this.position = position;  
  46. }  
  47. @Override  
  48. public String toString() {  
  49.     return "Employee [id=" + id + ", name=" + name + ", email=" + email + ", phone=" + phone + ", add_date="  
  50.             + add_date + ", position=" + position + "]";  
  51. }  
  52.   
  53. }  


页面类
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
            
            
public class Page < T > {
private int pageNo = 1 ; // 页码,默认是第一页
private int pageSize = 10 ; // 每页显示的记录数,默认是10
private int totalRecord ; // 总记录数
private int totalPage ; // 总页数
private boolean getAllRecord ; // 是否获取所有记录
private List < T > results ; // 对应的当前页记录
private Map < String , Object > params = new HashMap < String , Object >(); // 其他的参数我们把它分装成一个Map对象
public boolean isGetAllRecord () {
return getAllRecord ;
}
public void setGetAllRecord ( boolean getAllRecord ) {
this . getAllRecord = getAllRecord ;
}
public int getPageNo () {
return pageNo ;
}
public void setPageNo ( int pageNo ) {
this . pageNo = pageNo ;
}
public int getPageSize () {
return pageSize ;
}
public void setPageSize ( int pageSize ) {
this . pageSize = pageSize ;
}
public int getTotalRecord () {
return totalRecord ;
}
public void setTotalRecord ( int totalRecord ) {
this . totalRecord = totalRecord ;
// 在设置总页数的时候计算出对应的总页数,在下面的三目运算中加法拥有更高的优先级,所以最后可以不加括号.
int totalPage = totalRecord % pageSize == 0 ? totalRecord / pageSize
: totalRecord / pageSize + 1 ;
if ( totalPage == 0 )
totalPage = 1 ;
this . setTotalPage ( totalPage );
if ( this . pageNo <= 0 )
this . setPageNo ( 1 );
if ( this . pageNo > this . totalPage )
this . pageNo = this . totalPage ;
}
public int getTotalPage () {
return totalPage ;
}
public void setTotalPage ( int totalPage ) {
this . totalPage = totalPage ;
}
public List < T > getResults () {
return results ;
}
public void setResults ( List < T > results ) {
this . results = results ;
}
public Map < String , Object > getParams () {
return params ;
}
public void setParams ( Map < String , Object > params ) {
this . params = params ;
}
@Override
public String toString () {
StringBuilder builder = new StringBuilder ();
builder . append ( "Page [pageNo=" ). append ( pageNo ). append ( ", pageSize=" )
. append ( pageSize ). append ( ", results=" ). append ( results )
. append ( ", totalPage=" ). append ( totalPage )
. append ( ", totalRecord=" ). append ( totalRecord ). append ( "]" );
return builder . toString ();
}
}
 来自CODE的代码片
Page.java

2.创建服务方法service接口

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
           
           
public interface EmployeeService {
public int getListCount ( String name ); //获取列表所有信息数目
public List < Employee > getList ( Page < Employee > page , String name ); //通过姓名搜索员工
public void addEmployee ( Employee employee ); //添加员工
public void delEmployee ( String [] idArray ); //删除员工
public void updEmployee ( Employee employee ); //更新员工
}
 来自CODE的代码片
EmployeeService.java

3.创建方法与参数的映射关系Mapper接口

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
           
           
public interface AdsvEmployeeMapper {
public int getListCount ( @param ( "name" ) String name );
List < Employee > getList ( @param ( "begin" ) int pageNo , @param ( "size" ) int pageSize , @param ( "name" ) String name )
public void addEmployee ( Employee employee );
public void delEmployee ( @param ( "idArray" ) String [] idArray );
public void updEmployee ( Employee employee );
}
 来自CODE的代码片
EmployeeMapper.java

4.创建实体类方法实现类Impl实现service接口中的方法

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
           
           
@Service
public class EmployImpl implements EmpolyeeServise {
@Autowired
private EmployeeMapper mapper ;
public int getListCount ( String name ) {
return mapper . getListCount ( name );
}
public List < Employee > getList ( Page < Employee > page , String name ) {
return mapper . getList ( page . getPageNo (), page . getPageSize (), name );
}
public void addEmployee ( Employee employee ) {
mapper . addEmployee ( employee );
}
public void delEmployee ( String [] idArray ) {
mapper . delEmployee ( idArray );
}
public void updEmployee ( Employee employee ) {
mapper . updEmployee ( employee );
}
}
 来自CODE的代码片
EmployeeImpl.java

5.设置方法与数据库逻辑的Mapper.xml配置

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
           
           
<?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.extr.persistence.huajie.EmployeeMapper" >
<select id= "getListCount" resultType= "int" >
SELECT count(*) form employee // SQL语言操作,这里的employ代表数据库里的employee表
<where>
<if test= "name != null and '' !=name" >
and name like (concat('%',#{name},'%')
</if>
</where>
</select>
<select id= "getList" resultType= "com.extr.domain.huajie.Employee" >
SELECT
id,name,email,phone,add_date,position from employee
<where>
<if test= "name != null and '' != name" >
and name like concat(concat('%',#{name}),'%')
</if>
</where>
LIMIT #{begin},#{size}
</select>
<insert id= "addEmployee" resultType= "com.extr.domain.huajie.Employee"
useGeneratedKeys= "true" keyProperty= "id" >
INSERT into Employee (name,email,phone,add_date,position)value(#{name},#{email},#{phone},#{add_date},#{position})
</select>
<delete id= "delEmployee" parameterType= "java.lang.String" >
<![CDATA[
delete from employee
where
id in
]]>
<foreach collection= "idArray" item= "id" open= "(" separator= ","
close= ")" >
#{id}
</foreach>
</delete>
<update id= "updEmployee" parameterType= "com.extr.domain.huajie.AdsvEmployee" >
update employee
<set>
<if test= "name != null" >name=#{name}, </if>
<if test= "email != null" >email=#{email}, </if>
<if test= "phone != null" >phone=#{phone}, </if>
<if test= "add_date != null" >add_date=#{add_date}, </if>
<if test= "position != null" >position=#{position} </if>
</set>
<where>
id=#{id}
</where>
</update>
</mapper>
 来自CODE的代码片
EmployMapper.xml
6.编写逻辑层控制层control类

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
           
           
/**
* 员工管理控制类
*/
@Controller
public class EmployeeControl {
@Autowired
private EmpolyeeServise service ;
public static AdsvReturn adsvRetrun = new AdsvReturn (); //返回消息工具类
/**
* 界面跳转至用户管理界面
*
* @param model
* @param request
* @return String
*/
@RequestMapping ( value = { "admin/Employee" }, method = RequestMethod . GET ) //设置路径映射
public String adsvEmployee ( Model model , HttpServletRequest request ) {
return "admin/module/adsvEmployee" ;
}
/**
* 查询用户信息
* @param name
* @param request
* @return
*/
@RequestMapping ( value = { "admin/employeeList" }, method = RequestMethod . POST )
public @ResponseBody String showListPage ( String name , HttpServletRequest request ) {
try {
if ( null != request . getParameter ( "page" )) {
int index = Integer . parseInt ( request . getParameter ( "page" ));
int size = Integer . parseInt ( request . getParameter ( "rows" ));
Page < AdsvEmployee > page = new Page < AdsvEmployee >();
index = ( index - 1 ) * size ;
page . setPageNo ( index );
page . setPageSize ( size );
int count = service . getListCount ( name );
List < AdsvEmployee > list = service . getList ( page , name );
Map < String , Object > map = new HashMap < String , Object >();
map . put ( "total" , count );
map . put ( "rows" , list );
String json = JSON . toJSONString ( map );
return json ;
} else {
return null ;
}
} catch ( NumberFormatException e ) {
// TODO Auto-generated catch block
e . printStackTrace ();
return null ;
}
}
/**
* 添加用户
*
* @param name
* @param request
* @return
*/
@ResponseBody
@RequestMapping ( value = { "admin/addEmployee" }, method = RequestMethod . POST , produces = "text/html;charset=UTF-8" )
public String addEmployee ( AdsvEmployee employee , HttpServletRequest request ) {
try {
//添加员工的创建时间
employee . setAdd_date ( new Date ());
service . addEmployee ( employee );
adsvRetrun . addSuccess ();
return adsvRetrun . toString ();
} catch ( Exception e ) {
e . printStackTrace ();
adsvRetrun . addFail ( "" );
return adsvRetrun . toString ();
}
}
/**
* 删除用户
*
* @param name
* @param request
* @return
*/
@RequestMapping ( value = { "admin/delEmployee" }, method = RequestMethod . POST , produces = "text/html;charset=UTF-8" )
public @ResponseBody String delEmployee ( String ids , HttpServletRequest request ) {
String [] idArray = ids . split ( "," , 0 );
try {
service . delEmployee ( idArray );
adsvRetrun . delSuccess ();
return adsvRetrun . toString ();
} catch ( Exception e ) {
e . printStackTrace ();
adsvRetrun . delFail ( "" );
return adsvRetrun . toString ();
}
}
/**
* 修改用户
*
* @param name
* @param request
* @return
*/
@RequestMapping ( value = { "admin/updEmployee" }, method = RequestMethod . POST , produces = "text/html;charset=UTF-8" )
public @ResponseBody String updEmployee ( AdsvEmployee employee , HttpServletRequest request ) {
try {
service . updEmployee ( employee );
adsvRetrun . updSuccess ();
return adsvRetrun . toString ();
} catch ( Exception e ) {
e . printStackTrace ();
adsvRetrun . updFail ( "" );
return adsvRetrun . toString ();
}
}
}
 来自CODE的代码片
EmployeeControl.java

  • 4
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值