发表于: 2018-04-03 23:18:39
1 712
一、今天完成的事情
对照网上的案例实现JDBCTemplate。
首先创建好数据库表,导入maven引用包
添加实体用户类
package com.ptteng.vo;
public class User {
private int id;
private String name;
private int password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPassword() {
return password;
}
public void setPassword(int password) {
this.password = password;
}
}
package com.ptteng.dao;
import com.ptteng.vo.User;
import org.springframework.jdbc.core.RowMapper;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserMapper implements RowMapper<User> {
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getInt("password"));
return user;
}
}
添加用户DAO接口
package com.ptteng.dao;
import com.ptteng.vo.User;
import java.util.List;
import javax.sql.DataSource;
public interface UserDao {
/**
* This is the method to be used to initialize *这是用来初始化的方法
* database resources ie. connection. *数据库资源 即。连接。
*/
public void setDataSource(DataSource ds);
/**
* This is the method to be used to create *这是用来创建的方法
* a record in the Student table. Student表中的记录。
*/
public void create(String name, Integer password);
/**
* This is the method to be used to list down *这是用于列出的方法
* a record from the Student table corresponding *来自Student表的记录对应
* to a passed student id. *传给学生ID。
*/
public User getUser(Integer id);
/**
* This is the method to be used to list down这是用来列出的方法
* all the records from the Student table. *学生表中的所有记录。
*/
public List<User> listUser();
/**
* This is the method to be used to delete这是用来删除的方法
* a record from the Student table corresponding来自Student表的记录对应
* to a passed student id.传给学生ID。
*/
public void delete(Integer id);
/**
* This is the method to be used to update*这是用来更新的方法
* a record into the Student table. *记录到学生表中。
*/
public void update(Integer id, Integer password);
}
添加用户DAO接口实现类
package com.ptteng.impl;
import java.util.List;
import javax.sql.DataSource;
import com.ptteng.dao.UserDao;
import com.ptteng.dao.UserMapper;
import com.ptteng.vo.User;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserJDBCTemplate implements UserDao {
private DataSource dataSource;
private JdbcTemplate jdbcTemplateObject;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.jdbcTemplateObject = new JdbcTemplate(dataSource);
}
public void create(String name, Integer password) {
String SQL = "insert into temp (name, password) values (?,?)";
jdbcTemplateObject.update(SQL, name, password);
System.out.println("name:" + name + " password:" + password);
return;
}
public User getUser(Integer id) {
return null;
}
public User getStudent(Integer id) {
String SQL = "select * from temp where id = ?";
User user = jdbcTemplateObject.queryForObject(SQL,new Object[]{id}, new UserMapper());
return user;
}
public List<User> listUser() {
String SQL = "select * from temp";
List<User> users = jdbcTemplateObject.query(SQL,
new UserMapper());
return users;
}
public void delete(Integer id) {
String SQL = "delete from temp where id = ?";
jdbcTemplateObject.update(SQL, id);
System.out.println("Deleted Record with ID = " + id);
return;
}
public void update(Integer id, Integer password) {
String SQL = "update temp set password = ? where id = ?";
jdbcTemplateObject.update(SQL, password, id);
System.out.println("Updated Record with ID = " + id);
return;
}
}
配置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: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/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.ptteng.*" />
<!-- Initialization for data source -->
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!-- Definition for studentJDBCTemplate bean -->
<bean id="userJDBCTemplate"
class="com.ptteng.impl.UserJDBCTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
</beans>
对bean的几种注入方法,目前只会这一种属性注入,对于构造器,还不太理解
测试类
package com.ptteng.vo;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ptteng.impl.UserJDBCTemplate;
class UserStudent {
public static void main(String[] args) {
ApplicationContext context =
new ClassPathXmlApplicationContext("Beans.xml"); //类路径下直接通过ClassPathXmlApplicationContext对象来获取
UserJDBCTemplate userJDBCTemplate =
(UserJDBCTemplate) context.getBean("userJDBCTemplate");
System.out.println("------Records Creation--------");
userJDBCTemplate.create("Zara", 11);
userJDBCTemplate.create("Nuha", 2);
userJDBCTemplate.create("Ayan", 15);
System.out.println("------Listing Multiple Records--------");
List<User> users = userJDBCTemplate.listUser();
for (User record : users) {
System.out.print("ID : " + record.getId());
System.out.print(", Name : " + record.getName());
System.out.println(", Password : " + record.getPassword());
}
System.out.println("----Updating Record with ID = 2 -----");
userJDBCTemplate.update(1, 20);
System.out.println("----Listing Record with ID = 2 -----");
User user = userJDBCTemplate.getStudent(1);
System.out.print("ID : " + user.getId());
System.out.print(", Name : " + user.getName());
System.out.println(", Age : " + user.getPassword());
}
}
上面的分层是根据自己的理解自己瞎分的,也不知道我这样分层对不对。。
二、明天计划的事情:
对照教程的确是实现了操作数据库的功能,但是自己对里面的一些功能的实现还一知半解,计划明天继续学习spring的知识,将面向对象的知识再好好梳理一遍,类和对象,实现接口的方法,bean的注入方法等。
三、遇到的问题:
配置文件中,相邻的bean之间有其他字符
因为是从其他地方直接复制过来的,所以中间可能有一些空格符不被识别然后报错,删除了就好
四、收获:
理解了什么时候this.,什么情况new对象,构造方法什么的
类方法:
1.用static修饰的是类方法.
2.无论是实例方法还是类方法,在对象创建之后,都可以用“.”运算符调用这些方法。
3.JAVA语言中,类中的类方法,在该类加载到内存时,就分配了相应的入口地址,所以类方法不但可以被任何对象调用执行,也可以直接通过类名调用,入口地址直到程序退出的时候才会被取消。
4.因此JAVA语言允许通过类名直接调用类方法,而实例方法则不能通过类名调用。
5.而且类方法不能操作实例变量,也不能调用实例方法,因为在类创建对象之前,实例成员变量没有分配内存,而且实例方法也没有入口地址。
如果要在类方法中调用实例变量,可以先创建一个类的实例,然后通过实例访问该变量。
实例方法:
1.不用static修饰的是实例方法.
2.实例方法只能通过对象来调用,而不能通过类名来调用。
3.当this关键字出现在实例方法中时,代表正在调用该方法的当前对象.
当实例成员出现在实例方法中时,默认格式:this.成员变量
当类成员变量出现在实例方法中时,默认格式:类名.成员变量
构造方法:
1.构造方法负责对象成员的初始化工作,为实例变量赋予合适的初始值,具有特殊的声明规则.方法名和类名相同且没有返回值。 一般用 new 构造方法名() 来创建该类的对象,构造方法是在对象一建立就 运行,给对象初始化。
2.当this关键字出现在类的构造方法中时,代表使用该构造方法所创建的对象。
3.程序员如果没有在JAVA类中提供构造方法,系统则会自动提供一个无参数的构造方法,这个构造方法为空,不做任何事情。所以JAVA类中至少含有一个构造方法。
https://blog.csdn.net/douunderstand/article/details/52462700
进度:
任务开始时间:2018年3月1日
预计demo时间:2018年4月20日
是否延期:
延期理由:
评论