Netbeans--Spring--JdbcTemplate

     We need configure seven Bean components to use JdbcTemplate .Partly dataSource,transactionManager,userDAO,UserDAOProxy and Acton class.

    The detailed steps as following:

    Firstly,create dataSource in applicationContext.xml.This labes include four property,partly driverClassName,url,username,password.In addition,u should add tow class packages:commons-collections-3.1.jar,commons-pool-1.2.jar under  \\WEB-INF\\lib.

   <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destdestroy-method="close">
        <property name="driverClassName">
            <value>org.gjt.mm.mysql.Driver</value>
        </property>
        <property name="url">
            <value>jdbc:mysql://localhost:3306/demo</value>
        </property>
        <property name="username">
            <value>root</value>
        </property>
        <property name="password">
            <value>825384</value>
        </property>
    </bean>

    u need also configure transactionmanager  object

   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>
    </bean>

   Secondly,create POJO class named User.java under com.demo.spring.dao package for table user.

  public class User {
    protected String id;
    protected String username;
    protected String password;
    protected String email;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

  Thirdly,create data operation class com.demo.spring.dao.UserDAO.java

         3.1create function of isValid to adjust whether user login success.

         public boolean isValid(String username,String password){
        List userList=new ArrayList();
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);//create JdbcTemplate object
        userList=jdbcTemplate.query("SELECT \* FROM user WHERE username="+username+" 'and password='"+password+"'", new RowMapper(){
            public Object mapRow(ResultSet rs,int rowNum) throws SQLException{
                User user=new User();
                user.setId("ID");
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                return user;     //return object user.          
            }
        });
        if(userList.size()>0)
            return true;
        else
            return false;
    }

    3.2create function of isExist to adjust whether username is exist.

   public boolean isExist(String username){
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        int count=jdbcTemplate.queryForInt("SELECT count(\*) FROM user WHERE username='"+username+"'");
        if (count>0)
            return true;
        else
            return false;
    }

 3.3 add a new user using function of insertUser when register model.

  public void insertUser(User user){
        String username=user.getUsername();
        String password=user.getPassword();
        String email=user.getEmail();
        JdbcTemplate jt=new JdbcTemplate(getDataSource());
        jt.update("inser into user(username,password,email)value('"+username+"','"+password+"','"+email+"');");
    }

  Fourthly,configure UserDAO and its transaction agency.

    In order to use UserDAO.java class,u should configure <bean> object ,and inject datasource object for this class.

   <bean id="userADO" class="com.demo.spring.dao.UserDAO">
        <property name="dataSource">
            <ref local="dataSource"/>
        </property>
    </bean>

   In order to implement UserDAO's transaction management function,u should confiugre a transaction agency object UserDAOProxy.

<bean id="UserDAOProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
        <property name="transactionManager">
            <ref bean="transactionManager" />
        </property>
        <property name="target">
            <ref local="userDAO"/>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="insert \*">PROPAGATION_REQUIRED</prop>
                <prop key="\*">PROPAGATION_REQUIRED,readOnly</prop>
            </props>
        </property>
    </bean>

  Fifthly,modify LoginAction class to access UserDAO and validate login

     add a variable as following:

 UserDAO userDAO;

    public UserDAO getUserDAO() {
        return userDAO;
    }

    public void setUserDAO(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

   inject userDAO object into loginAction under applicationContext.xml

<bean id="loginAction"
    class="com.demo.spring.actions.LoginAction">
        <property name="commandClass">
            <value>com.demo.spring.forms.LoginForm</value>
        </property>
        <property name="userDAO">
            <ref local="userDAO"/>
        </property>
        <property name="validator">
            <ref local="loginValidator" />
        </property>
        <!-- return the failure page -->
        <property name="formView">
            <value>login</value>
        </property>
        <!--return the success page-->
        <property name="successView">
            <value>welcome</value>
        </property>
    </bean>

Reference:《开发者突击:java web主流框架整合开发 j2ee+Structs+Hibernate+Spring》 刘中兵编著  ,电子工业出版社


Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
About

Wei Guan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
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
   
       
Today