Skip to content

Commit 59cf3e3

Browse files
committed
[add]添加domain dao service层
1 parent eceb6f6 commit 59cf3e3

File tree

168 files changed

+46810
-52
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

168 files changed

+46810
-52
lines changed

spring-boot-shiro/pom.xml

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,27 @@
2525
</properties>
2626

2727
<dependencies>
28+
<!--<dependency>-->
29+
<!--<groupId>org.springframework.boot</groupId>-->
30+
<!--<artifactId>spring-boot-starter-data-jpa</artifactId>-->
31+
<!--</dependency>-->
32+
<!--<dependency>-->
33+
<!--<groupId>org.springframework.boot</groupId>-->
34+
<!--<artifactId>spring-boot-starter-jdbc</artifactId>-->
35+
<!--</dependency>-->
2836
<dependency>
2937
<groupId>org.springframework.boot</groupId>
30-
<artifactId>spring-boot-starter-data-jpa</artifactId>
31-
</dependency>
32-
<dependency>
33-
<groupId>org.springframework.boot</groupId>
34-
<artifactId>spring-boot-starter-jdbc</artifactId>
38+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
3539
</dependency>
3640
<dependency>
3741
<groupId>org.springframework.boot</groupId>
38-
<artifactId>spring-boot-starter-thymeleaf</artifactId>
42+
<artifactId>spring-boot-starter-web</artifactId>
3943
</dependency>
44+
45+
<!-- aop依赖 -->
4046
<dependency>
4147
<groupId>org.springframework.boot</groupId>
42-
<artifactId>spring-boot-starter-web</artifactId>
48+
<artifactId>spring-boot-starter-aop</artifactId>
4349
</dependency>
4450

4551
<dependency>

spring-boot-shiro/src/main/java/com/example/springbootshiro/SpringBootShiroApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.example.springbootshiro;
22

3+
import org.mybatis.spring.annotation.MapperScan;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
6+
import org.springframework.transaction.annotation.EnableTransactionManagement;
57

68
@SpringBootApplication
9+
@EnableTransactionManagement
10+
@MapperScan("com.example.springbootshiro.dao")
711
public class SpringBootShiroApplication {
812

913
public static void main(String[] args) {
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
package com.example.springbootshiro.dao;
1+
package com.example.springbootshiro.config;
22

33
import tk.mybatis.mapper.common.Mapper;
44
import tk.mybatis.mapper.common.MySqlMapper;
55

66
/**
77
* Created by Douglee on 2018/3/29.
88
*/
9-
public interface CommonMapper<T> extends Mapper<T>, MySqlMapper<T> {
9+
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
1010
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.example.springbootshiro.controller;
2+
3+
import com.example.springbootshiro.domain.UserInfo;
4+
import com.github.pagehelper.PageInfo;
5+
import org.apache.shiro.SecurityUtils;
6+
import org.apache.shiro.authc.AuthenticationToken;
7+
import org.apache.shiro.session.Session;
8+
import org.apache.shiro.subject.Subject;
9+
import org.springframework.stereotype.Controller;
10+
11+
import java.util.HashMap;
12+
import java.util.Map;
13+
14+
/**
15+
* Created by Douglee on 2018/3/30.
16+
*/
17+
public class BaseController {
18+
19+
protected Map<String, Object> getDataTable(PageInfo<?> pageInfo){
20+
Map<String, Object> rspData = new HashMap<>();
21+
rspData.put("rows", pageInfo.getList());
22+
rspData.put("total", pageInfo.getTotal());
23+
return rspData;
24+
}
25+
public static Subject getSubject() {
26+
return SecurityUtils.getSubject();
27+
}
28+
29+
protected UserInfo getCurrentUser() {
30+
return (UserInfo) getSubject().getPrincipal();
31+
}
32+
33+
protected Session getSession() {
34+
return getSubject().getSession();
35+
}
36+
37+
protected Session getSession(Boolean flag) {
38+
return getSubject().getSession(flag);
39+
}
40+
41+
protected void login(AuthenticationToken token) {
42+
getSubject().login(token);
43+
}
44+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.example.springbootshiro.controller;
2+
3+
import com.example.springbootshiro.domain.ResponseResult;
4+
import com.example.springbootshiro.domain.UserInfo;
5+
import com.example.springbootshiro.service.UserService;
6+
import com.example.springbootshiro.util.MD5Utils;
7+
import com.example.springbootshiro.util.StringUtils;
8+
import org.apache.shiro.authc.*;
9+
import org.apache.shiro.session.Session;
10+
import org.springframework.beans.factory.annotation.Autowired;
11+
import org.springframework.stereotype.Controller;
12+
import org.springframework.ui.Model;
13+
import org.springframework.web.bind.annotation.GetMapping;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.RequestMapping;
16+
import org.springframework.web.bind.annotation.ResponseBody;
17+
18+
/**
19+
* Created by Douglee on 2018/3/30.
20+
*/
21+
@Controller
22+
public class LoginController extends BaseController {
23+
24+
@Autowired
25+
private UserService userService;
26+
27+
@GetMapping("/login")
28+
public String login() {
29+
return "login";
30+
}
31+
32+
@PostMapping("/login")
33+
@ResponseBody
34+
public ResponseResult login(String username, String password, String code, Boolean rememberMe) {
35+
if (StringUtils.isNullorEmpty(code)) {
36+
return ResponseResult.warn("验证码不能为空!");
37+
}
38+
// Session session = super.getSession();
39+
// String sessionCode = (String) session.getAttribute("_code");
40+
// if (!code.toLowerCase().equals(sessionCode)) {
41+
// return ResponseResult.warn("验证码错误!");
42+
// }
43+
44+
password = MD5Utils.encrypt(username.toLowerCase(), password);
45+
UsernamePasswordToken token = new UsernamePasswordToken(username, password, rememberMe);
46+
47+
try {
48+
super.login(token);
49+
this.userService.updateLoginTime(username);
50+
return ResponseResult.ok();
51+
} catch (AuthenticationException e) {
52+
return ResponseResult.error("认证失败!");
53+
} catch (Exception e) {
54+
return ResponseResult.error(e.getMessage());
55+
}
56+
}
57+
@RequestMapping("/")
58+
public String redirectIndex() {
59+
return "redirect:/index";
60+
}
61+
62+
@GetMapping("/403")
63+
public String forbid() {
64+
return "403";
65+
}
66+
67+
@RequestMapping("/index")
68+
public String index(Model model) {
69+
UserInfo user = super.getCurrentUser();
70+
model.addAttribute("user", user);
71+
return "index";
72+
}
73+
74+
}

spring-boot-shiro/src/main/java/com/example/springbootshiro/dao/MenuMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.example.springbootshiro.dao;
22

3+
import com.example.springbootshiro.config.MyMapper;
34
import com.example.springbootshiro.domain.MenuInfo;
45

56
import java.util.List;
67

78
/**
89
* Created by Douglee on 2018/3/29.
910
*/
10-
public interface MenuMapper extends CommonMapper<MenuMapper> {
11+
public interface MenuMapper extends MyMapper<MenuInfo> {
1112

1213
List<MenuInfo> findUserPermissions(String userName);
1314

spring-boot-shiro/src/main/java/com/example/springbootshiro/dao/RoleMapper.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.example.springbootshiro.dao;
22

3+
import com.example.springbootshiro.config.MyMapper;
34
import com.example.springbootshiro.domain.RoleInfo;
45

56
import java.util.List;
67

78
/**
89
* Created by Douglee on 2018/3/29.
910
*/
10-
public interface RoleMapper extends CommonMapper<RoleInfo> {
11+
public interface RoleMapper extends MyMapper<RoleInfo> {
1112

1213
List<RoleInfo> findUserRole(String userName);
1314

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.example.springbootshiro.dao;
22

3+
import com.example.springbootshiro.config.MyMapper;
34
import com.example.springbootshiro.domain.RoleMenu;
45

56
/**
67
* Created by Douglee on 2018/3/29.
78
*/
8-
public interface RoleMenuMapper extends CommonMapper<RoleMenu> {
9+
public interface RoleMenuMapper extends MyMapper<RoleMenu> {
910
}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.example.springbootshiro.dao;
22

3+
import com.example.springbootshiro.config.MyMapper;
34
import com.example.springbootshiro.domain.UserInfo;
45

5-
import java.util.List;
6-
76
/**
87
* Created by Douglee on 2018/3/29.
98
*/
10-
public interface UserMapper extends CommonMapper<UserInfo> {
9+
public interface UserMapper extends MyMapper<UserInfo> {
1110

1211
UserInfo findUserProfile(UserInfo user);
1312
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.example.springbootshiro.dao;
22

3+
import com.example.springbootshiro.config.MyMapper;
34
import com.example.springbootshiro.domain.UserRole;
45

56
/**
67
* Created by Douglee on 2018/3/29.
78
*/
8-
public interface UserRoleMapper extends CommonMapper<UserRole> {
9+
public interface UserRoleMapper extends MyMapper<UserRole> {
910
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.example.springbootshiro.domain;
2+
3+
import java.util.HashMap;
4+
5+
/**
6+
* Created by Douglee on 2018/3/30.
7+
*/
8+
public class ResponseResult extends HashMap<String, Object> {
9+
10+
private static final long serialVersionUID = -556622754874885339L;
11+
12+
private static final Integer SUCCESS = 0;
13+
private static final Integer WARN = 0;
14+
private static final Integer FAIL = 500;
15+
16+
public ResponseResult() {
17+
put("code", SUCCESS);
18+
put("msg", "操作成功");
19+
}
20+
21+
public static ResponseResult error(Object msg) {
22+
ResponseResult responseResult = new ResponseResult();
23+
responseResult.put("msg", msg);
24+
responseResult.put("code", FAIL);
25+
26+
return responseResult;
27+
}
28+
29+
public static ResponseResult warn(Object msg) {
30+
ResponseResult responseResult = new ResponseResult();
31+
responseResult.put("msg", msg);
32+
responseResult.put("code", WARN);
33+
34+
return responseResult;
35+
}
36+
37+
public static ResponseResult ok(Object msg) {
38+
ResponseResult responseResult = new ResponseResult();
39+
responseResult.put("msg", msg);
40+
responseResult.put("code", SUCCESS);
41+
42+
return responseResult;
43+
}
44+
45+
public static ResponseResult ok() {
46+
return new ResponseResult();
47+
}
48+
49+
@Override
50+
public ResponseResult put(String key, Object value) {
51+
super.put(key, value);
52+
return this;
53+
}
54+
}

spring-boot-shiro/src/main/java/com/example/springbootshiro/domain/UserInfo.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class UserInfo implements Serializable {
2020
@Id
2121
@GeneratedValue(generator = "JDBC")
2222
@Column(name = "USER_ID")
23-
private Integer userId;
23+
private Long userId;
2424

2525
@Column(name = "USERNAME")
2626
private String username;
@@ -43,11 +43,11 @@ public class UserInfo implements Serializable {
4343
@Transient
4444
private String roleName;
4545

46-
public Integer getUserId() {
46+
public Long getUserId() {
4747
return userId;
4848
}
4949

50-
public void setUserId(Integer userId) {
50+
public void setUserId(Long userId) {
5151
this.userId = userId;
5252
}
5353

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package com.example.springbootshiro.service;
2+
3+
import com.example.springbootshiro.service.IService;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.transaction.annotation.Propagation;
6+
import org.springframework.transaction.annotation.Transactional;
7+
import tk.mybatis.mapper.common.Mapper;
8+
import tk.mybatis.mapper.entity.Example;
9+
10+
import java.util.List;
11+
12+
/**
13+
* Created by Douglee on 2018/3/30.
14+
*/
15+
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
16+
public abstract class BaseService<T> implements IService<T> {
17+
18+
@Autowired
19+
protected Mapper<T> mapper;
20+
21+
public Mapper<T> getMapper() {
22+
return mapper;
23+
}
24+
25+
public List<T> selectAll() {
26+
return mapper.selectAll();
27+
}
28+
29+
public T selectByKey(Object key) {
30+
return mapper.selectByPrimaryKey(key);
31+
}
32+
33+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
34+
public int save(T entity) {
35+
return mapper.insert(entity);
36+
}
37+
38+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
39+
public int delete(Object key) {
40+
return mapper.deleteByPrimaryKey(key);
41+
}
42+
43+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
44+
public int batchDelete(List<String> list, String property, Class<T> clazz) {
45+
Example example = new Example(clazz);
46+
example.createCriteria().andIn(property, list);
47+
48+
return this.mapper.deleteByExample(example);
49+
}
50+
51+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
52+
public int updateAll(T entity) {
53+
return mapper.updateByPrimaryKey(entity);
54+
}
55+
56+
@Transactional(propagation = Propagation.REQUIRED, readOnly = false)
57+
public int updateNotNull(T entity) {
58+
return mapper.updateByPrimaryKeySelective(entity);
59+
}
60+
61+
public List<T> selectByExample(Object example) {
62+
return mapper.selectByExample(example);
63+
}
64+
}

0 commit comments

Comments
 (0)