发表于: 2017-10-27 23:34:02
1 596
day9
今日完成
找了个教程照着敲了一遍,终于能运行出结果了
项目结构如下
dao接口
public interface UserDao {
List<User> findAllUser();
void create(String id, String name,String password);
void execInsert(String sql);
}
dao实现类
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
//创建一个findAllUser()方法,返回类型是list,list元素类型是User。
public List<User> findAllUser(){
String sql = "select * from myspringjdbcdb";
final List<User> listAllUser = new ArrayList<User>();
jdbcTemplate.query(sql, new RowCallbackHandler() {
public void processRow(ResultSet resultSet) throws SQLException {
User u=new User();
u.setuName(resultSet.getString("u_name"));
u.setuPassword(resultSet.getString("u_password"));
u.setuId(resultSet.getString("u_id"));
listAllUser.add(u);
}
});
return listAllUser;
}
public void create(String id, String name,String password) {
String SQL = "insert into myspringjdbcdb (u_id, u_name, u_password) values (?, ?, ?)";
jdbcTemplate.update(SQL, id, name,password);
System.out.println("Created Record Id = " + id + " Name = " +name + "Password = " + password);
}
public void execInsert(String sql) {
jdbcTemplate.execute(sql);
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
}
pojo类,set和get方法略
public class User {
private String uId;
private String uName;
private String uPassword;
private UserDao dao;
配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/mydb</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>612049</value>
</property>
</bean>
<bean id="userDAO" class="impl.UserDaoImpl ">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
<bean id="user" class="pojo.User">
<property name="dao">
<ref bean="userDAO"/>
</property>
</bean>
</beans>
遇到的困难
1,Spring中提示
cvc-complex-type.3.2.2: 元素 'ref' 中不允许出现属性 'local'。
解决方法:
1)将 ref 子标签 作为property父标签的 属性之一,属性值即为原local属性的值
<property name="dataSource">
<ref local="dataSource"/>
</property>
改成下面
<property name="dataSource" ref="dataSource"/>
2)把local属性改为bean属性
<bean id="service" class="service.ServiceImpl">
<property name="dao">
<ref bean="dao"/>
</property>
</bean>
2,xmlbeanfactory过时了
1)原方法已过时
Resource resource = new ClassPathResource("applicationContext.xml"); //装载配置文件
BeanFactory factory = new XmlBeanFactory(resource);
2)新方法用一个语句,取代了原来的两个语句。
BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext.xml");
3,始终不能理解下面配置文件中的语句含义
<bean id="userDAO" class="impl.UserDaoImpl ">
<property name="jdbcTemplate">
<ref bean="jdbcTemplate"/>
</property>
</bean>
明日计划
感觉有很多基础知识都不懂,Java的好多类的功能不了解,要去学习基础了。
希望能捋捋思路,然后自己写一个出来。
收获
了解了下xml语法的大概规则。
粗浅的了解下RowMapper:
简单讲就是用来把数据库中的列字段和java bean中属性对应上,这样就可以赋值了。也像JDBC中的bean.setName(rs.getString("name"); Spring把这段代码抽象出来写成RowMapper。
评论