Skip to content

Commit 13d245d

Browse files
committed
Spring jdbc & transaction review.
1 parent 3b549c9 commit 13d245d

14 files changed

+928
-274
lines changed

.idea/artifacts/rainbow_war_exploded.xml

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__org_aspectj_aspectjrt_1_8_10.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/libraries/Maven__org_aspectj_aspectjweaver_1_8_10.xml

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

Lines changed: 419 additions & 273 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,17 @@
8181
<version>1.2.24</version>
8282
</dependency>
8383

84+
<dependency>
85+
<groupId>org.aspectj</groupId>
86+
<artifactId>aspectjrt</artifactId>
87+
<version>1.8.10</version>
88+
</dependency>
8489

90+
<dependency>
91+
<groupId>org.aspectj</groupId>
92+
<artifactId>aspectjweaver</artifactId>
93+
<version>1.8.10</version>
94+
</dependency>
8595

8696
</dependencies>
8797
</project>

rainbow.iml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
2525
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
2626
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
27+
<excludeFolder url="file://$MODULE_DIR$/target" />
2728
</content>
2829
<orderEntry type="inheritedJdk" />
2930
<orderEntry type="sourceFolder" forTests="false" />
@@ -56,6 +57,8 @@
5657
<orderEntry type="library" name="Maven: redis.clients:jedis:2.6.0" level="project" />
5758
<orderEntry type="library" name="Maven: org.apache.commons:commons-pool2:2.0" level="project" />
5859
<orderEntry type="library" name="Maven: com.alibaba:fastjson:1.2.24" level="project" />
60+
<orderEntry type="library" name="Maven: org.aspectj:aspectjrt:1.8.10" level="project" />
61+
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.8.10" level="project" />
5962
</component>
6063
<component name="TemplatesService">
6164
<option name="TEMPLATE_FOLDERS">

src/main/java/dao/StudentDao.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package dao;
2+
3+
import domain.Student;
4+
5+
import javax.sql.DataSource;
6+
import java.util.List;
7+
8+
/**
9+
* Created by pengfei on 2017/9/20.
10+
*/
11+
public interface StudentDao {
12+
13+
public void setDataSource(DataSource dataSource);
14+
15+
public void create(String name,String male,int age);
16+
17+
public Student findStudent(int id);
18+
19+
public List<Student> listStudents();
20+
21+
public void delete(int id);
22+
23+
public void update(int id,int age);
24+
25+
public Student getStudentByCall(int id);
26+
27+
public void transactionTest(int id,String name, String male);
28+
29+
public void declarativeTxTest(int age);
30+
31+
}

src/main/java/dao/StudentDaoImpl.java

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package dao;
2+
3+
import domain.Student;
4+
import domain.StudentMapper;
5+
import org.apache.log4j.Logger;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.dao.DataAccessException;
8+
import org.springframework.jdbc.core.JdbcTemplate;
9+
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
10+
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
11+
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
12+
import org.springframework.stereotype.Repository;
13+
import org.springframework.transaction.PlatformTransactionManager;
14+
import org.springframework.transaction.TransactionDefinition;
15+
import org.springframework.transaction.TransactionStatus;
16+
import org.springframework.transaction.support.DefaultTransactionDefinition;
17+
18+
import javax.sql.DataSource;
19+
import java.util.List;
20+
import java.util.Map;
21+
22+
/**
23+
* Created by pengfei on 2017/9/20.
24+
*/
25+
26+
@Repository
27+
public class StudentDaoImpl implements StudentDao {
28+
29+
private final static Logger logger = Logger.getLogger(StudentDaoImpl.class);
30+
31+
private DataSource dataSource;
32+
private JdbcTemplate template;
33+
34+
@Autowired
35+
private PlatformTransactionManager txManager;
36+
37+
38+
@Autowired
39+
public void setDataSource(DataSource dataSource) {
40+
this.dataSource = dataSource;
41+
this.template = new JdbcTemplate(dataSource);
42+
}
43+
44+
public void create(String name, String male, int age) {
45+
String sql = "insert into student (name, male,age) values(?, ?, ?)";
46+
int rows = this.template.update(sql, name, male, age);
47+
logger.info("Create a student in db, affect rows:" + rows);
48+
49+
}
50+
51+
public Student findStudent(int id) {
52+
String sql = "select * from student where id = ?";
53+
Student student = (Student) this.template.queryForObject(sql, new StudentMapper(), id);
54+
logger.info("get student with id:" + id);
55+
return student;
56+
}
57+
58+
public List<Student> listStudents() {
59+
String sql = "select * from student";
60+
List<Student> students = this.template.query(sql, new StudentMapper());
61+
62+
logger.info("Got students");
63+
return students;
64+
}
65+
66+
public void delete(int id) {
67+
String sql = "delete from student where id= ?";
68+
this.template.update(sql, id);
69+
logger.info("Delete student with id:" + id);
70+
}
71+
72+
public void update(int id, int age) {
73+
String sql = "update student set age =? where id= ?";
74+
this.template.update(sql, age, id);
75+
76+
}
77+
78+
public Student getStudentByCall(int id) {
79+
SimpleJdbcCall call = new SimpleJdbcCall(this.dataSource).withProcedureName("getStudentRecord");
80+
SqlParameterSource in = new MapSqlParameterSource("in_id", id);
81+
82+
Map<String, Object> out = call.execute(in);
83+
84+
Student student = new Student();
85+
student.setName((String) out.get("out_name"));
86+
student.setAge((Integer) out.get("out_age"));
87+
88+
return student;
89+
}
90+
91+
public void transactionTest(int id, String name, String male) {
92+
TransactionDefinition def = new DefaultTransactionDefinition();
93+
TransactionStatus status = txManager.getTransaction(def);
94+
95+
try {
96+
String sql1 = "update student set name =? where id= ?";
97+
98+
this.template.update(sql1, name, id);
99+
100+
String sql2 = "insert into student (name, male) values (?, ?) ";
101+
this.template.update(sql2, name, male);
102+
103+
// //Test transaction function.
104+
// if (id % 2 == 1)
105+
// throw new RecoverableDataAccessException("");
106+
107+
txManager.commit(status);
108+
logger.info("Transaction test success in dao");
109+
} catch (DataAccessException e) {
110+
logger.error("error found in TX execution.");
111+
txManager.rollback(status);
112+
throw e;
113+
}
114+
115+
return;
116+
}
117+
118+
public void declarativeTxTest(int age) {
119+
logger.info("dao tx begin");
120+
String sql = "insert into student (name, age) values(?, ?)";
121+
this.template.update(sql, "mary dianna", age);
122+
if (age < 18)
123+
throw new RuntimeException();
124+
logger.info("dao tx end");
125+
}
126+
}

src/main/java/domain/Student.java

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package domain;
2+
3+
/**
4+
* Created by pengfei on 2017/9/20.
5+
*/
6+
public class Student {
7+
private int id;
8+
private String name;
9+
private String male;
10+
private String comment;
11+
private double salary;
12+
private int age;
13+
14+
public Student() {
15+
16+
}
17+
18+
public Student(int age, String name, String male) {
19+
this.age = age;
20+
this.name = name;
21+
this.male = male;
22+
}
23+
24+
public int getAge() {
25+
return age;
26+
}
27+
28+
public void setAge(int age) {
29+
this.age = age;
30+
}
31+
32+
public double getSalary() {
33+
return salary;
34+
}
35+
36+
public void setSalary(double salary) {
37+
this.salary = salary;
38+
}
39+
40+
public int getId() {
41+
return id;
42+
}
43+
44+
public void setId(int id) {
45+
this.id = id;
46+
}
47+
48+
public String getName() {
49+
return name;
50+
}
51+
52+
public void setName(String name) {
53+
this.name = name;
54+
}
55+
56+
public String getMale() {
57+
return male;
58+
}
59+
60+
public void setMale(String male) {
61+
this.male = male;
62+
}
63+
64+
public String getComment() {
65+
return comment;
66+
}
67+
68+
public void setComment(String comment) {
69+
this.comment = comment;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
String s=this.getId()+" "+getName()+" "+getAge()+" "+getMale()+" "+getComment();
75+
return s;
76+
}
77+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package domain;
2+
3+
import org.springframework.jdbc.core.RowMapper;
4+
5+
import java.sql.ResultSet;
6+
import java.sql.SQLException;
7+
8+
/**
9+
* Created by pengfei on 2017/9/20.
10+
*/
11+
public class StudentMapper implements RowMapper<Student> {
12+
public Student mapRow(ResultSet rs, int i) throws SQLException {
13+
14+
Student student = new Student();
15+
student.setId(rs.getInt("id"));
16+
student.setName(rs.getString("name"));
17+
student.setAge(rs.getInt("age"));
18+
student.setMale(rs.getString("male"));
19+
student.setSalary(rs.getDouble("salary"));
20+
student.setComment(rs.getString("comment"));
21+
22+
return student;
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package service;
2+
3+
import domain.Student;
4+
5+
import java.util.List;
6+
7+
/**
8+
* Created by pengfei on 2017/9/20.
9+
*/
10+
public interface IStudentService {
11+
public void insertStudent(Student student);
12+
13+
public void updateStudent(int id, int age);
14+
15+
public List<Student> getAllStudent();
16+
17+
public void deleteStudent(int id);
18+
19+
public Student queryStudent(int id);
20+
21+
public Student queryStudentByCall(int id);
22+
23+
public void testTxManager(int id, String name, String male);
24+
25+
public void testDeclarativeTx(int age);
26+
27+
}

0 commit comments

Comments
 (0)