发表于: 2017-10-26 19:48:14

2 625


今天学习的内容:

学习了用JDBCTemplate对数据库表进行处理:

User类:

package jdbcTemplate.bean;

public class User {
private int id;
private String username;
private String password;

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;
}

@Override
   public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
public void UserMod(int id,String username,String password) {
this.id = id;
this.username = username;
this.password = password;
}
}

UserDao接口:

package jdbcTemplate.implement;

import jdbcTemplate.bean.User;

import java.util.List;

public interface UsersDao {
public void update(String name,int id);
}

UserImpl类:

package jdbcTemplate.implement;

import jdbcTemplate.bean.User;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;

public class UsersImpl implements UsersDao{


public void update(String username,int id){

String sql = "update user set username=? where id = ?";
template.update(sql,username,id);

}

}

配置applicationContext.xml:

<?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:aop="http://www.springframework.org/schema/aop"
      xmlns:tx="http://www.springframework.org/schema/tx"
      xmlns:context="http://www.springframework.org/schema/context"
      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">
<!--引入外部属性文件↓-->
   <context:property-placeholder location="classpath:jdbc.properties"/>
<!--连接数据库的配置-->

   <!--声明bean,,,未将DataSource声明为Bean,则无法使用数据库事务.-->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
</bean>

<!--配置jdbcTemplate,并装配DataSource数据源属性  -->
   <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>

<bean id="user" class="jdbcTemplate.bean.User"></bean>
<bean id="usersImpl" class="jdbcTemplate.implement.UsersImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

</beans>


测试类:

import jdbcTemplate.implement.UsersDao;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UpdateTest {

ClassPathXmlApplicationContext appConetxt = new ClassPathXmlApplicationContext("applicationContext.xml");
UsersDao studentsDAO = appConetxt.getBean(UsersDao.class);
int id = 3;
@Test
       public void testUpdate() throws Exception {
studentsDAO.update("wpj",3);
}

}


问题:在写applicationContext.xml的时候,总是出现红色的,问了下师兄,他说要让pom.xml应用一下,然后就解决了。还有执行的时候找不到该类,问了下师兄,把test的颜色改成了绿色,就可以了。

老是出现如下错误:

没有写出来更新的。

明天继续学习。


返回列表 返回列表
评论

    分享到