Skip to content

Commit 5dc6e8a

Browse files
committed
add areaservice
1 parent 7509c29 commit 5dc6e8a

9 files changed

Lines changed: 261 additions & 9 deletions

File tree

‎README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# myrep
2+
spring boot test project
3+
4+
```sql
5+
/*
6+
Navicat MySQL Data Transfer
7+
8+
Source Server : ubuntu
9+
Source Server Version : 50721
10+
Source Host : 127.0.0.1:13306
11+
Source Database : test
12+
13+
Target Server Type : MYSQL
14+
Target Server Version : 50721
15+
File Encoding : 65001
16+
17+
Date: 2018-04-18 10:28:02
18+
*/
19+
20+
SET FOREIGN_KEY_CHECKS=0;
21+
22+
-- ----------------------------
23+
-- Table structure for tb_area
24+
-- ----------------------------
25+
DROP TABLE IF EXISTS `tb_area`;
26+
CREATE TABLE `tb_area` (
27+
`area_id` int(2) NOT NULL AUTO_INCREMENT,
28+
`area_name` varchar(200) NOT NULL,
29+
`priority` int(2) NOT NULL DEFAULT '0',
30+
`create_time` datetime DEFAULT NULL,
31+
`last_edit_time` datetime DEFAULT NULL,
32+
PRIMARY KEY (`area_id`),
33+
KEY `uniq_name` (`area_name`)
34+
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
35+
36+
-- ----------------------------
37+
-- Records of tb_area
38+
-- ----------------------------
39+
INSERT INTO `tb_area` VALUES ('1', '东苑', '2', null, null);
40+
INSERT INTO `tb_area` VALUES ('2', '北苑', '1', null, null);
41+
42+
```

‎src/main/java/com/example/demo/config/dao/SessionFactoryConfiguration.java‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import org.mybatis.spring.SqlSessionFactoryBean;
44
import org.springframework.beans.factory.annotation.Autowired;
5-
import org.springframework.beans.factory.annotation.Qualifier;
65
import org.springframework.beans.factory.annotation.Value;
76
import org.springframework.context.annotation.Bean;
87
import org.springframework.context.annotation.Configuration;
@@ -24,9 +23,11 @@ public class SessionFactoryConfiguration {
2423
@Value("${entity_package}")
2524
private String entityPackage;
2625

27-
@Autowired
28-
@Qualifier("dataSource")
29-
private DataSource dataSource;
26+
private final DataSource dataSource;
27+
28+
public SessionFactoryConfiguration(DataSource dataSource) {
29+
this.dataSource = dataSource;
30+
}
3031

3132
@Bean(name="sqlSessionFactory")
3233
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.demo.config.service;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
5+
import org.springframework.transaction.PlatformTransactionManager;
6+
import org.springframework.transaction.annotation.EnableTransactionManagement;
7+
import org.springframework.transaction.annotation.TransactionManagementConfigurer;
8+
9+
import javax.sql.DataSource;
10+
11+
@Configuration
12+
@EnableTransactionManagement
13+
public class TransactionManagementConfiguration implements TransactionManagementConfigurer {
14+
15+
private final DataSource dataSource;
16+
17+
public TransactionManagementConfiguration(DataSource dataSource) {
18+
this.dataSource = dataSource;
19+
}
20+
21+
public PlatformTransactionManager annotationDrivenTransactionManager() {
22+
return new DataSourceTransactionManager(dataSource);
23+
}
24+
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.example.demo.dao;
22

33
import com.example.demo.entity.Area;
4+
import org.springframework.stereotype.Repository;
45

56
import java.util.List;
67

8+
@Repository(value = "areaDao")
79
public interface AreaDao {
810
List<Area> queryArea();
911
Area queryAreaById(int areaId);
1012
int insertArea(Area area);
1113
int updateArea(Area area);
12-
int deleteArea(Area area);
14+
int deleteArea(int areaId);
1315
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo.service;
2+
3+
import com.example.demo.entity.Area;
4+
5+
import java.util.List;
6+
7+
public interface AreaService {
8+
List<Area> queryArea();
9+
Area queryAreaById(int areaId);
10+
boolean insertArea(Area area);
11+
boolean updateArea(Area area);
12+
boolean deleteArea(int areaId);
13+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.example.demo.service.impl;
2+
3+
import com.example.demo.dao.AreaDao;
4+
import com.example.demo.entity.Area;
5+
import com.example.demo.service.AreaService;
6+
import org.springframework.stereotype.Service;
7+
import org.springframework.transaction.annotation.Transactional;
8+
9+
import java.util.Date;
10+
import java.util.List;
11+
12+
@Service
13+
public class AreaServiceImpl implements AreaService {
14+
15+
private final AreaDao areaDao;
16+
17+
public AreaServiceImpl(AreaDao areaDao) {
18+
this.areaDao = areaDao;
19+
}
20+
21+
@Override
22+
public List<Area> queryArea() {
23+
return areaDao.queryArea();
24+
}
25+
26+
@Override
27+
public Area queryAreaById(int areaId) {
28+
return areaDao.queryAreaById(areaId);
29+
}
30+
31+
@Transactional
32+
@Override
33+
public boolean insertArea(Area area) {
34+
if (area.getAreaName() != null && "".equals(area.getAreaName())){
35+
area.setCreateTime(new Date());
36+
area.setLastEditTime(new Date());
37+
try {
38+
int effectedNumber = areaDao.insertArea(area);
39+
if (effectedNumber > 0) {
40+
return true;
41+
}
42+
throw new RuntimeException("插入失败");
43+
} catch (Exception e) {
44+
throw new RuntimeException("插入失败:" + e.getMessage());
45+
}
46+
}
47+
throw new RuntimeException("插入失败:信息为空" );
48+
}
49+
50+
@Override
51+
public boolean updateArea(Area area) {
52+
if (area.getAreaId() != null && area.getAreaId() > 0){
53+
area.setLastEditTime(new Date());
54+
try {
55+
int effectedNumber = areaDao.updateArea(area);
56+
if (effectedNumber > 0) {
57+
return true;
58+
}
59+
throw new RuntimeException("更新失败");
60+
} catch (Exception e) {
61+
throw new RuntimeException("更新失败:" + e.getMessage());
62+
}
63+
}
64+
throw new RuntimeException("更新失败:信息为空" );
65+
}
66+
67+
@Override
68+
public boolean deleteArea(int areaId) {
69+
if (areaId >0 ){
70+
try {
71+
int effectedNumber = areaDao.deleteArea(areaId);
72+
if (effectedNumber > 0) {
73+
return true;
74+
}
75+
throw new RuntimeException("删除失败");
76+
} catch (Exception e) {
77+
throw new RuntimeException("删除失败:" + e.getMessage());
78+
}
79+
}
80+
throw new RuntimeException("删除失败:信息为空" );
81+
}
82+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.example.demo.web;
2+
3+
import com.example.demo.entity.Area;
4+
import com.example.demo.service.AreaService;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import java.util.HashMap;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
@RestController
14+
@RequestMapping("/admin")
15+
public class AreaController {
16+
private final AreaService areaService;
17+
18+
public AreaController(AreaService areaService) {
19+
this.areaService = areaService;
20+
}
21+
22+
@GetMapping(value = "/list")
23+
private Map<String, Object> listarea(){
24+
Map<String, Object> modelMap = new HashMap<>();
25+
List<Area> list = areaService.queryArea();
26+
modelMap.put("areaList", list);
27+
return modelMap;
28+
}
29+
}
Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
server.port=8082
2+
server.servlet.context-path=/demo
23

34
jdbc.driver=com.mysql.jdbc.Driver
4-
jdbc.url=jdbc:mysql://10.0.2.15:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
5+
jdbc.url=jdbc:mysql://127.0.0.1:13306/test?useUnicode=true&characterEncoding=utf8&useSSL=false
56
jdbc.username=root
6-
jbdc.password=123456
7+
jdbc.password=123456
78

89
#Mybatis
9-
mybatis_config_file=mybatis_config.xml
10-
mappler_path=/mapper/**.xml
10+
mybatis_config_file=mybatis-config.xml
11+
mapper_path=/mapper/**.xml
1112
entity_package=com.example.demo.entity
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.example.demo.dao;
2+
3+
import com.example.demo.entity.Area;
4+
import org.junit.Test;
5+
import org.junit.runner.RunWith;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.test.context.junit4.SpringRunner;
9+
10+
import java.util.Date;
11+
import java.util.List;
12+
13+
import static org.junit.Assert.*;
14+
15+
@RunWith(SpringRunner.class)
16+
@SpringBootTest
17+
public class AreaDaoTest {
18+
@Autowired
19+
private AreaDao areaDao;
20+
21+
@Test
22+
public void queryArea() {
23+
List<Area> areaList = areaDao.queryArea();
24+
assertEquals(2, areaList.size());
25+
}
26+
27+
@Test
28+
public void queryAreaById() {
29+
Area area = areaDao.queryAreaById(1);
30+
assertEquals("东苑", area.getAreaName());
31+
}
32+
33+
@Test
34+
public void insertArea() {
35+
Area area = new Area();
36+
area.setAreaName("南苑");
37+
area.setPriority(1);
38+
int effectedNum = areaDao.insertArea(area);
39+
assertEquals(1, effectedNum);
40+
}
41+
42+
@Test
43+
public void updateArea() {
44+
Area area = new Area();
45+
area.setAreaId(2);
46+
area.setAreaName("西苑");
47+
area.setPriority(3);
48+
area.setLastEditTime(new Date());
49+
int effectedNum = areaDao.updateArea(area);
50+
assertEquals(1, effectedNum);
51+
}
52+
53+
@Test
54+
public void deleteArea() {
55+
int effectedNum = areaDao.deleteArea(1);
56+
assertEquals(1, effectedNum);
57+
}
58+
}

0 commit comments

Comments
 (0)