myshop 概述
myshop项目是根据视频教程 java 单体应用 做的一个练习项目,目前完成了登录功能、用户管理、类别管理后续有时间会继续做其它的功能。趁着双11花了99元一年买了台阿里云服务器,演示地址>> myshop
项目地址 https://gitee.com/yehuabin/myshop
项目结构
my-shop-dependencies:管理jar包的版本,所有项目依赖此项目
my-shop-commons:项目通用工具、实体类
my-shop-domain: pojo类,对于数据库一张表
my-shop-web-admin:后台管理功能
my-shop-web-api、my-shop-web-ui:商城前端展示部分,目前还没开发
basedao
1
2
3
4
5
6
|
public interface basedao<t> {
int create(t model);
int update(t model);
int delete( int id);
t getbyid( int id);
}
|
basedao定义了通用的数据操作接口其它具体的操作接口都继承该dao
1
2
3
4
5
|
public interface tbuserdao extends basedao<tbuser> {
tbuser getbyusernameandpasswrod(map<string,string> param);
list<tbuser> getbypage(pagequery<tbuserquery> pagequery);
int getcount(pagequery<tbuserquery> pagequery);
}
|
tbuserdao继承basedao并定义了自己特有的一些接口
baseservice
1
2
3
4
5
6
|
public interface baseservice<t extends baseentity> {
baseresult create(t model);
t getbyid( int id);
baseresult update(t model);
baseresult delete( int id);
}
|
baseservice定义通用的服务接口,t 就是某个具体的domain对象
然后给这个通用服务接口定义一个通用的实现 baseserviceimpl。实现通用的crud
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
|
public abstract class baseserviceimpl<t extends baseentity,d extends basedao<t>> implements baseservice<t> {
@autowired
protected d dao;
@override
public baseresult create(t model) {
baseresult baseresult = entityvalidator.validate(model);
if (!baseresult.issuccess()) {
return baseresult;
}
model.setcreated( new date());
model.setupdated( new date());
return getbaseresult(dao.create(model), "新增失败" );
}
public t getbyid( int id) {
return dao.getbyid(id);
}
public baseresult update(t model)
{
baseresult baseresult = entityvalidator.validate(model);
if (!baseresult.issuccess()) {
return baseresult;
}
model.setupdated( new date());
return getbaseresult(dao.update(model), "更新失败" );
}
public baseresult delete( int id) {
return getbaseresult(dao.delete(id), "删除失败" );
}
private baseresult getbaseresult( int row,string error){
baseresult baseresult = baseresult.success();
if (row<= 0 ){
baseresult=baseresult.fail(error);
}
return baseresult;
}
}
|
用户服务继承baseservice并扩展了两个方法
1
2
3
4
|
public interface userservice extends baseservice<tbuser> {
pageresult<tbuser> getbypage(pagequery<tbuserquery> pagequery);
tbuser login(string username, string password);
}
|
用户服务的具体实现
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
|
@service
public class userserviceimpl extends baseserviceimpl<tbuser,tbuserdao> implements userservice {
@override
public baseresult create(tbuser tbuser) {
if (stringutils.isblank(tbuser.getpassword())) {
return baseresult.fail( "密码不能为空" );
}
tbuser.setpassword(md5digestashex(tbuser.getpassword()));
return super .create(tbuser);
}
@override
public pageresult<tbuser> getbypage(pagequery<tbuserquery> pagequery) {
pageresult<tbuser> result = new pageresult<>();
list<tbuser> list = dao.getbypage(pagequery);
int count=dao.getcount(pagequery);
result.setdata(list);
result.setrecordstotal(count);
result.setrecordsfiltered(count);
return result;
}
@override
public baseresult update(tbuser tbuser) {
if (stringutils.isnotblank(tbuser.getpassword())) {
tbuser.setpassword(md5digestashex(tbuser.getpassword()));
}
return super .update(tbuser);
}
@override
public tbuser login(string username, string password) {
map<string, string> param = new hashmap<>();
param.put( "username" , username);
param.put( "password" , md5digestashex(password));
system.out.println(param.get( "password" ));
return dao.getbyusernameandpasswrod(param);
}
private string md5digestashex(string password){
return digestutils.md5digestashex(password.getbytes());
}
}
|
basecontroller
basecontroller定义了通用的列表页、表单页、删除操作的跳转
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
|
public abstract class basecontroller<t extends baseentity,s extends baseservice<t>> {
@autowired
protected s service;
@modelattribute
public t getmodel(integer id) {
t model=initformentity();
if (id != null && id > 0 ) {
model = service.getbyid(id);
}
return model;
}
@requestmapping ( "/list" )
public string list(model model) {
return getjsppath()+ "/list" ;
}
@requestmapping (value = { "/create" , "edit" }, method = requestmethod.get)
public string form() {
return getjsppath()+ "/form" ;
}
@requestmapping ( "/detail" )
public string detail() {
return getjsppath()+ "/detail" ;
}
@responsebody
@requestmapping ( "/delete" )
public baseresult delete( int id) {
return service.delete(id);
}
@requestmapping (value = { "/create" , "edit" }, method = requestmethod.post)
public string form(t entity, model model) {
baseresult baseresult;
if (entity.getid() != null && entity.getid() > 0 ) {
baseresult = service.update(entity);
} else {
baseresult = service.create(entity);
}
//新增或者修改成功返回列表页
if (baseresult.issuccess()) {
return "redirect:list" ;
}
//失败显示错误信息
model.addattribute( "result" , baseresult);
return getjsppath()+ "/form" ;
}
protected abstract t initformentity();
protected string getjsppath(){
return getclass().getsimplename().tolowercase().replace( "controller" , "" );
}
}
|
任何其它模块只要继承basecontroller,就可以自动跳转到列表、表单、删除等页面,可以大大减少代码量
具体的usercontroller只需要实现特有的业务逻辑就可以
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@controller
@requestmapping ( "/user" )
public class usercontroller extends basecontroller<tbuser,userservice> {
@responsebody
@requestmapping ( "/page" )
public pageresult<tbuser> page(pagequery<tbuserquery> pagequery, tbuserquery query) {
pagequery.setquery(query);
pageresult<tbuser> tbusers = service.getbypage(pagequery);
return tbusers;
}
@override
protected tbuser initformentity() {
return new tbuser();
}
}
|
总结
以上所述是小编给大家介绍的spring+springmvc+mybatis 开发java单体应用,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!
原文链接:https://www.cnblogs.com/yehuabin/p/9993497.html