Spring 注解整合 spring+mybatis+struts2

[1].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
action 部分:
public class UserAction extends ActionSupport {
     @Autowired
     private UserManger userService;
     private User       user;
     private List<User> userList;
     
     public String execute() throws Exception {
         return null ;
     }
     
     public String login() {
         if (user != null ) {
             User user2 = userService.login(user);
             if (user2 != null ) {
                 return SUCCESS;
             }
         }
         this .addFieldError( "user.username" , "用户名或密码错误!" );
         return INPUT;
     }
     
     public String addUI() {
         return "addUser" ;
     }
     
     public String updateUI() {
         user = userService.findUserById(user.getId());
         return "updateUser" ;
     }
     
     public String add() {
         userService.addUser(user);
         return SUCCESS;
     }
     
     public String delete() {
         userService.deleteUser(user.getId());
         return SUCCESS;
     }
     
     public String update() {
         userService.updateUser(user);
         return SUCCESS;
     }
     
     public User getUser() {
         return user;
     }
     
     public void setUser(User user) {
         this .user = user;
     }
     
     public String queryAllUser() {
         userList = userService.findAllUser();
         return "userList" ;
     }
     
     public List<User> getUserList() {
         return userList;
     }
     
     public void setUserList(List<User> userList) {
         this .userList = userList;
     }
     
}

[2].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
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
UserDao 部分:
 
@Repository
public class UserDao {
     private final String       INSERT_USER     = "insertUser" ;
     private final String       UPDATE_USER     = "updateUser" ;
     private final String       DELETE_USER     = "deleteUser" ;
     private final String       FIND_USER_BYID  = "findUserById" ;
     private final String       SELECT_ALL_USER = "selectAllUser" ;
     private final String       USER_LOGIN      = "userLogin" ;
     @Autowired
     private SqlSessionTemplate sqlSessionTemplate;
     
     public void insertUser(User user) {
         sqlSessionTemplate.insert(INSERT_USER, user);
     }
     
     public void updateUser(User user) {
         sqlSessionTemplate.update(UPDATE_USER, user);
     }
     
     public void deleteUser(Integer userId) {
         sqlSessionTemplate.delete(DELETE_USER, userId);
     }
     
     public User findUserByid(Integer userId) {
         return sqlSessionTemplate.selectOne(FIND_USER_BYID, userId);
     }
     
     public List<User> findAll() {
         return sqlSessionTemplate.selectList(SELECT_ALL_USER);
     }
     
     public User userLogin(User user) {
         return sqlSessionTemplate.selectOne(USER_LOGIN, user);
     }
}

[3].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
model:
public class User implements Serializable {
     /**
      *
      */
     private static final long serialVersionUID = 1L;
     private int               id;
     private String            username;
     private String            password;
     private String            email;
     
     public int getId() {
         return id;
     }
     
     public void setId( int id) {
         this .id = id;
     }
     
     public String getUsername() {
         return username;
     }
     
     public void setUsername(String username) {
         this .username = username;
     }
     
     public String getPassword() {
         return password;
     }
     
     public void setPassword(String password) {
         this .password = password;
     }
     
     public String getEmail() {
         return email;
     }
     
     public void setEmail(String email) {
         this .email = email;
     }
     
     @Override
     public int hashCode() {
         final int prime = 31 ;
         int result = 1 ;
         result = prime * result + ((email == null ) ? 0 : email.hashCode());
         result = prime * result + id;
         result = prime * result + ((password == null ) ? 0 : password.hashCode());
         result = prime * result + ((username == null ) ? 0 : username.hashCode());
         return result;
     }
     
     @Override
     public boolean equals(Object obj) {
         if ( this == obj)
             return true ;
         if (obj == null )
             return false ;
         if (getClass() != obj.getClass())
             return false ;
         final User other = (User) obj;
         if (email == null ) {
             if (other.email != null )
                 return false ;
         } else if (!email.equals(other.email))
             return false ;
         if (id != other.id)
             return false ;
         if (password == null ) {
             if (other.password != null )
                 return false ;
         } else if (!password.equals(other.password))
             return false ;
         if (username == null ) {
             if (other.username != null )
                 return false ;
         } else if (!username.equals(other.username))
             return false ;
         return true ;
     }
     
}

[4].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
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
sqlmapper(sql映射):
 
<?xml version= "1.0" encoding= "utf-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace= "你的实体类的全路径" >
     
     <resultMap type= "你的实体类的全路径" id= "userResult" >
         <result property= "id" column= "id" jdbcType= "INTEGER" javaType= "java.lang.Integer" />
         <result property= "username" column= "username" />
         <result property= "password" column= "password" />
     </resultMap>
     <select id= "userLogin"  parameterType= "user" resultMap= "userResult" >
         select * from user
         where
             username=#{username} and password=#{password}
     </select>
 
     <select id= "selectAllUser" resultMap= "userResult" >
         select * from user
     </select>
 
     <select id= "findUserById" parameterType= "int" resultMap= "userResult" >
         select *
         from user where id=#{id}
     </select>
 
     <insert id= "insertUser" parameterType= "user" >
      <![CDATA[
         insert into
         user(username,password) values(#{username},#{password})
         ]]>
     </insert>
 
     <update id= "updateUser" parameterType= "user" >
         update user set
         username=#{username},password=#{password} where id=#{id}
     </update>
     
     <delete id= "deleteUser" parameterType= "int" >
         delete from user where
         id=#{id}
     </delete>
 
</mapper>

[5].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
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
UserManger:
@Repository
@Transactional
public class UserManger {
     @Autowired
     private UserDao userDao;
     
     public void addUser(User user) {
         userDao.insertUser(user);
     }
     
     public void updateUser(User user) {
         userDao.updateUser(user);
     }
     
     public void deleteUser(Integer userId) {
         userDao.deleteUser(userId);
     }
     
     public User findUserById(Integer userId) {
         return userDao.findUserByid(userId);
     }
     
     public List<User> findAllUser() {
         return userDao.findAll();
     }
     
     public User login(User user) {
         return userDao.userLogin(user);
     }
}

[6].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
applicationContext.xml(spring配置文件):
<?xml version= "1.0" encoding= "UTF-8" ?>
<beans xmlns= "http://www.springframework.org/schema/beans"
     xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context= "http://www.springframework.org/schema/context"
     xmlns:aop= "http://www.springframework.org/schema/aop"
     xmlns:tx= "http://www.springframework.org/schema/tx"
     xsi:schemaLocation="http: //www.springframework.org/schema/beans
            http: //www.springframework.org/schema/beans/spring-beans-3.0.xsd
            http: //www.springframework.org/schema/aop
            http: //www.springframework.org/schema/aop/spring-aop-3.0.xsd
            http: //www.springframework.org/schema/tx
            http: //www.springframework.org/schema/tx/spring-tx-3.0.xsd
            http: //www.springframework.org/schema/context
            http: //www.springframework.org/schema/context/spring-context-3.0.xsd">
     
     <!-- 采用注释的方式配置bean -->
     <context:annotation-config />
     <!-- 配置要扫描的包 -->
     <context:component-scan base- package = "cn.ac.sec" ></context:component-scan>
     
     <!--proxy-target- class = "true" 强制使用cglib代理   如果为 false 则spring会自动选择-->
     <aop:aspectj-autoproxy  proxy-target- class = "true" />
     
     <!-- 数据库配置文件位置 -->
     <context:property-placeholder location= "classpath:jdbc.properties" />
     
     <!-- 配置dbcp数据源 -->
     <bean id= "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" destroy-method= "close" >
         <property name= "driverClassName" value= "${jdbc.driverClassName}" />
         <property name= "url" value= "${jdbc.url}" />
         <property name= "username" value= "${jdbc.username}" />
         <property name= "password" value= "${jdbc.password}" />
         <!-- 队列中的最小等待数 -->
         <property name= "minIdle" value= "${jdbc.minIdle}" ></property>
         <!-- 队列中的最大等待数 -->
         <property name= "maxIdle" value= "${jdbc.maxIdle}" ></property>
         <!-- 最长等待时间,单位毫秒 -->
         <property name= "maxWait" value= "${jdbc.maxWait}" ></property>
         <!-- 最大活跃数 -->
         <property name= "maxActive" value= "${jdbc.maxActive}" ></property>
         <property name= "initialSize" value= "${jdbc.initialSize}" ></property>
     </bean>
     
     <!-- 配置mybitasSqlSessionFactoryBean -->
     <bean id= "sqlSessionFactory" class = "org.mybatis.spring.SqlSessionFactoryBean" >
         <property name= "dataSource" ref= "dataSource" />
         <property name= "configLocation" value= "classpath:mybatis.xml" ></property>
     </bean>
     
     <!-- 配置SqlSessionTemplate -->
     <bean id= "sqlSessionTemplate" class = "org.mybatis.spring.SqlSessionTemplate" >
         <constructor-arg name= "sqlSessionFactory" ref= "sqlSessionFactory" />
     </bean>
     
     <!-- 事务配置 -->
     <bean id= "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" >
         <property name= "dataSource" ref= "dataSource" />
     </bean>
     
     <!-- 使用annotation注解方式配置事务 -->
     <tx:annotation-driven transaction-manager= "transactionManager" />
 
</beans>

[7].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
1
2
3
4
5
6
7
8
9
10
jdbc.propers:
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql: //localhost:3306/operationLog
jdbc.username=root
jdbc.password=root
jdbc.maxActive = 2
jdbc.maxIdle = 5
jdbc.minIdle= 1
jdbc.initialSize = 3
jdbc.maxWait = 3000

[8].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
1
2
3
4
5
6
7
8
9
10
11
Mybitis映射:
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" >
<configuration>
     <typeAliases>
         <typeAlias alias= "user" type= "cn.ac.sec.model.User" />
     </typeAliases>
     <mappers>
         <mapper resource= "你的sqlMapper.xml文件" />
     </mappers>
</configuration> 

[9].[代码] [Java]代码 跳至 [1] [2] [3] [4] [5] [6] [7] [8] [9]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Struts2 配置文件:
<?xml version= "1.0" encoding= "UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
<struts> 
 
     < package name= "user" namespace= "/user" extends = "struts-default" >
         <action name= "user_*" class = "全类名" method= "{1}" >
             <result name= "success" type= "redirectAction" >user_queryAllUser.action</result>
             <result name= "input" >/index.jsp</result>
             <result name= "userList" >/userList.jsp</result>
             <result name= "addUser" >/userAdd.jsp</result>
             <result name= "updateUser" >/userUpdate.jsp</result>
         </action>
     </ package >
</struts>   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值