Skip to content

Commit b4a8e80

Browse files
committed
fix sql
1 parent 7e53289 commit b4a8e80

File tree

8 files changed

+146
-11
lines changed

8 files changed

+146
-11
lines changed

.idea/compiler.xml

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

mvc.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
</configuration>
3030
</facet>
3131
</component>
32-
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
32+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
3333
<output url="file://$MODULE_DIR$/target/classes" />
3434
<output-test url="file://$MODULE_DIR$/target/test-classes" />
3535
<content url="file://$MODULE_DIR$">
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.lj.spring.bean;/**
2+
* @Author 段连洁 [ manajay.dlj@gmail.com ]
3+
* @Date 05/08/2017 4:39 PM
4+
*/
5+
6+
/**
7+
* @author 段连洁 [manajay.dlj@gmail.com]
8+
* @enum Sex
9+
* @description user sex
10+
* @date 05/08/2017 4:39 PM
11+
*/
12+
public enum Sex {
13+
MALE(1,"男"), FEMALE(0,"女");
14+
15+
/**
16+
* @author 段连洁 [ manajay.dlj@gmail.com ]
17+
* @date 05/08/2017 4:40 PM
18+
*/
19+
private int i;
20+
private String name;
21+
22+
private Sex(int i, String name) {
23+
this.i = i;
24+
this.name = name;
25+
}
26+
27+
public int getI() {
28+
return i;
29+
}
30+
31+
public void setI(int i) {
32+
this.i = i;
33+
}
34+
35+
public String getName() {
36+
return name;
37+
}
38+
39+
public void setName(String name) {
40+
this.name = name;
41+
}
42+
43+
public static Sex getSex(int id) {
44+
if (id == 0){
45+
return FEMALE;
46+
} else if (id == 1) {
47+
return MALE;
48+
}
49+
50+
return null;
51+
}
52+
53+
54+
@Override
55+
public String toString() {
56+
return "Sex{" +
57+
"i=" + i +
58+
", name='" + name + '\'' +
59+
'}';
60+
}
61+
}

src/main/java/com/lj/spring/bean/User.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,26 @@ public class User {
2727
*/
2828
private String detail;
2929

30+
private Sex sex;
31+
3032
public User() {
3133
}
3234

33-
public User(int id, String name, int age, String password, String detail) {
34-
this(name,age,password,detail);
35-
this.id = id;
35+
public User(String name, int age, String password, String detail, Sex sex) {
36+
this.name = name;
37+
this.age = age;
38+
this.password = password;
39+
this.detail = detail;
40+
this.sex = sex;
3641
}
3742

38-
public User(String name, int age, String password, String detail) {
43+
public User(int id, String name, int age, String password, String detail, Sex sex) {
44+
this.id = id;
3945
this.name = name;
4046
this.age = age;
4147
this.password = password;
4248
this.detail = detail;
49+
this.sex = sex;
4350
}
4451

4552
public int getId() {
@@ -82,6 +89,14 @@ public void setDetail(String detail) {
8289
this.detail = detail;
8390
}
8491

92+
public Sex getSex() {
93+
return sex;
94+
}
95+
96+
public void setSex(Sex sex) {
97+
this.sex = sex;
98+
}
99+
85100
@Override
86101
public String toString() {
87102
return "User{" +
@@ -90,6 +105,7 @@ public String toString() {
90105
", age=" + age +
91106
", password='" + password + '\'' +
92107
", detail='" + detail + '\'' +
108+
", sex=" + sex +
93109
'}';
94110
}
95111
}

src/main/java/com/lj/spring/controller/UserController.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.lj.spring.controller;
22

3+
import com.lj.spring.bean.Sex;
34
import com.lj.spring.bean.User;
45
import com.lj.spring.service.UserService;
56
import org.apache.log4j.Logger;
@@ -49,6 +50,10 @@ public ModelAndView goToWelcome(){
4950

5051
@RequestMapping(value = "/register")
5152
public ModelAndView register (User user) {
53+
if (user.getSex() == null){
54+
user.setSex(Sex.MALE);
55+
}
56+
5257
userService.addUser(user);
5358
return showList();
5459
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.lj.spring.typeHandler;
2+
3+
/**
4+
* @Author 段连洁 [ manajay.dlj@gmail.com ]
5+
* @Date 05/08/2017 5:14 PM
6+
*/
7+
8+
import com.lj.spring.bean.Sex;
9+
import org.apache.ibatis.type.JdbcType;
10+
import org.apache.ibatis.type.TypeHandler;
11+
12+
import java.sql.CallableStatement;
13+
import java.sql.PreparedStatement;
14+
import java.sql.ResultSet;
15+
import java.sql.SQLException;
16+
17+
/**
18+
* @author 段连洁 [manajay.dlj@gmail.com]
19+
* @class SexTypeHandler
20+
* @description user sex type handler
21+
* @date 05/08/2017 5:14 PM
22+
*/
23+
public class SexTypeHandler implements TypeHandler<Sex> {
24+
25+
@Override
26+
public void setParameter(PreparedStatement ps, int i, Sex sex, JdbcType jdbcType) throws SQLException {
27+
ps.setInt(i,sex.getI());
28+
}
29+
30+
@Override
31+
public Sex getResult(ResultSet rs, int columnIndex) throws SQLException {
32+
int id = rs.getInt(columnIndex);
33+
return Sex.getSex(id);
34+
}
35+
36+
@Override
37+
public Sex getResult(ResultSet rs, String columnName) throws SQLException {
38+
int id = rs.getInt(columnName);
39+
return Sex.getSex(id);
40+
}
41+
42+
@Override
43+
public Sex getResult(CallableStatement cs, int columnIndex) throws SQLException {
44+
int id = cs.getInt(columnIndex);
45+
return Sex.getSex(id);
46+
}
47+
}

src/main/resources/config/spring-mybatis.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
1010

1111

12+
<bean id="typeHandler" class="com.lj.spring.typeHandler.SexTypeHandler"/>
13+
14+
1215
<bean id="datasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
1316
<property name="driverClassName" value="${db.driver}"/>
1417
<property name="url" value="${db.url}"/>
@@ -24,6 +27,7 @@
2427
p:dataSource-ref="datasource"
2528
p:typeAliasesPackage="com.lj.spring.bean"
2629
p:mapperLocations="classpath:mapper/*.xml"
30+
p:typeHandlers-ref="typeHandler"
2731
/>
2832

2933
<!--

src/main/resources/mapper/UserMapper.xml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@
1010
<result column="age" property="age" jdbcType="INTEGER"></result>
1111
<result column="password" property="password" jdbcType="VARCHAR"></result>
1212
<result column="detail" property="detail" jdbcType="VARCHAR"></result>
13+
<result column="sex" property="sex" jdbcType="INTEGER" typeHandler="com.lj.spring.typeHandler.SexTypeHandler"></result>
1314
</resultMap>
1415

1516
<sql id="User_Colum_List">
16-
id,name,age,password,detail
17+
id,name,age,password,detail,sex
1718
</sql>
1819

1920
<insert id="addUser" useGeneratedKeys="true" keyProperty="id" parameterType="User" >
20-
INSERT INTO users (name,age,password,detail) VALUES (#{name},#{age},#{password},#{detail})
21+
INSERT INTO users (name,age,password,detail,sex) VALUES (#{name},#{age},#{password},#{detail},#{sex,typeHandler=com.lj.spring.typeHandler.SexTypeHandler})
2122
</insert>
2223

2324
<!--resultType="User"-->
@@ -45,7 +46,8 @@
4546
<if test="name != null">name=#{name},</if>
4647
<if test="age != null">age=#{age},</if>
4748
<if test="password != null">password=#{password},</if>
48-
<if test="detail != null">detail=#{detail}</if>
49+
<if test="detail != null">detail=#{detail},</if>
50+
<if test="sex != null">sex=#{sex,typeHandler=com.lj.spring.typeHandler.SexTypeHandler}</if>
4951
</set>
5052
WHERE id=#{id}
5153
</update>

0 commit comments

Comments
 (0)