发表于: 2017-06-15 19:28:16

2 1232


今天完成了任务17的一半。(编写DAO,分别JdbcTemplate和Mybatis连接数据库,注意使用JDBCTemplate的时候分离Interface和Imple,使用Mybatis的时候注意理解为什么不需要Impl,注意遵守命名规范。)

main:

————————————————————————————————————————————————————

public class JdbcTemplateApp1 {


public static void main(String[] args) {

//create application context

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-cp.xml");

//create the bean (interface)

UserDao userDao = (UserDao) ctx.getBean("userDao");

//add a user

User testUser = new User();

testUser.setName("New Person");

userDao.createUser(testUser);

//get and print all users

List<User> userList = userDao.getAllUsers();

for(User user : userList){

System.out.println(user);

}

((ClassPathXmlApplicationContext) ctx).close();

}


}

——————————————————————————————————————————————————————

User:省略(一堆getter 和 setter)

——————————————————————————————————————————————————————

Dao interface:

public interface UserDao {

public void setDataSource(DataSource dataSource);

public boolean createUser(User user);

public User getUser(Long id);

public List<User> getAllUsers();

public boolean deleteUser(User user);

public boolean updateUser(User user);

public void cleanup();

}

——————————————————————————————————————————————————————

Dao Implementation:

public class UserDaoImpl implements UserDao {

private JdbcTemplate jdbcTemplate;


public void setDataSource(DataSource dataSource) {

jdbcTemplate = new JdbcTemplate(dataSource);

}


public boolean createUser(User user) {

String sqlQuery = "INSERT INTO user_information (name) VALUES ('" + user.getName() + "');";

jdbcTemplate.execute(sqlQuery);

return false;

}


public User getUser(Long id) {

// TODO Auto-generated method stub

return null;

}


public List<User> getAllUsers() {

String sqlQuery = "SELECT * FROM user_information";

List<User> userList = jdbcTemplate.query(sqlQuery, new UserRowMapper());

return userList;

}


public boolean deleteUser(User user) {

// TODO Auto-generated method stub

return false;

}


public boolean updateUser(User user) {

// TODO Auto-generated method stub

return false;

}


public void cleanup() {

// TODO Auto-generated method stub


}


}

————————————————————————————————————————————————————

RowMapper:

public class UserRowMapper implements RowMapper<User> {


public User mapRow(ResultSet rs, int rowNum) throws SQLException {

User user = new User();

user.setId(rs.getLong("id"));

user.setDeclaration(rs.getString("declaration"));

user.setLink(rs.getString("link"));

user.setMentor(rs.getString("mentor"));

user.setName(rs.getString("name"));

user.setQq(rs.getLong("qq"));

user.setRegisterTime(rs.getString("register_time"));

user.setSchool(rs.getString("school"));

user.setType(rs.getString("type"));

user.setWay(rs.getString("way"));

user.setCreateAt(rs.getLong("create_at"));

user.setUpdateAt(rs.getLong("update_at"));

return user;

}


}

————————————————————————————————————————————————————————

beans.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.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">



<bean id="ds" class="org.apache.commons.dbcp2.BasicDataSource">

<property name="username" value="${jdbc.username}"></property>

<property name="password" value="${jdbc.password}"></property>

<property name="driverClassName" value="${jdbc.driverClassName}">

</property>

<property name="url" value="${jdbc.url}"></property>

</bean>

<bean id="userDao"

class="spring_jdbc_template_demo_1.UserDaoImpl">

<property name="dataSource" ref="ds"></property>

</bean>

<context:property-placeholder location="user.properties" />

</beans>

————————————————————————————————————————————————————————

——————————————————————————————————————————————————————

结果:

六月 15, 2017 7:21:14 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh

信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@533ddba: startup date [Thu Jun 15 19:21:14 CST 2017]; root of context hierarchy

六月 15, 2017 7:21:14 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions

信息: Loading XML bean definitions from class path resource [beans-cp.xml]

User [name=童志荣, id=1, qq=602840329, type=java工程师, school=上海应用技术学院, registerTime=2017年5月8日, declaration=老大最帅, mentor=成都分院-李昂, way=知乎]

User [name=王振, id=2, qq=1129802181, type=前端工程师, school=防灾科技学院, registerTime=2017年5月16日, declaration=选定一个职业不容易,我一定会努力的改变自己, mentor=JS-刘其勇, way=知乎]

User [name=韩筠宜, id=3, qq=512952376, type=前端工程师, school=河海大学, registerTime=2017年5月18日, declaration=3个月后拿到offer, mentor=[深圳分院|内门弟子]CSS-覃腾波 , way=知乎]

User [name=杨纲, id=4, qq=993631441, type=前端工程师, school=内蒙古大学, registerTime=2017年5月21日, declaration=如果我不能在IT特训营拼尽全力,为自己以后的修行路上打好基础,就让我变胖2斤!, mentor=杨梦桐, way=朋友推荐]

User [name=张雪飞, id=5, qq=649924507, type=前端工程师, school=无, registerTime=2016年5月23日, declaration=现在的生活不是我想要的,我要改变!, mentor=张珺琢, way=知乎]

User [name=李博文, id=6, qq=187003594, type=前端工程师, school=吉林化工学院, registerTime=2017年3月20日, declaration=如果我不能在IT特训营拼尽全力,为自己以后的修行路上打好基础,让别人踩一辈子!, mentor=高高, way=知乎]

User [name=王立志, id=7, qq=981754163, type=前端工程师, school=台州市新桥高级中学, registerTime=2017年3月22日, declaration=如果我不能在IT特训营拼尽全力,那就让我永远找不到女朋友!, mentor=陈鹏康, way=知乎]

User [name=党楚翔, id=8, qq=15044774767, type=JAVA工程师, school=内蒙古电子信息职业技术学院, registerTime=2017年4月10日, declaration=如果我不能在IT特训营拼尽全力,为自己以后的修行路上打好基础,就让我永远追不到喜欢的人,给不了她想要的生活!, mentor=无, way=知乎]

User [name=杨舜, id=9, qq=591080675, type=JAVA工程师, school=河南工业职业技术学院, registerTime=2017年4月28日, declaration=如果我不能在IT特训营拼尽全力,那就让我永远找不到女朋友!, mentor=李瀚, way=知乎]

User [name=杨梦桐, id=10, qq=1371042472, type=前端工程师, school=北京化工大学, registerTime=2017年5月2日, declaration=If I look back, I am lost., mentor=王力, way=知乎]

User [name=New Person, id=11, qq=0, type=null, school=null, registerTime=null, declaration=null, mentor=null, way=null]

六月 15, 2017 7:21:15 下午 org.springframework.context.support.ClassPathXmlApplicationContext doClose

信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@533ddba: startup date [Thu Jun 15 19:21:14 CST 2017]; root of context hierarchy

——————————————————————————————————————————————————————————————


明天:争取完成任务17的另一半。继续学习Spring。


问题:在参考一个师兄的日报时,很多地方和师兄方法不一样,还有些没见过的东西。对DataSource是什么不太理解,看了下Doc也看不太懂。对那个RowMapper interface十分不理解,那个 rowNum 在method中根本就没用到,为什么是一个参数,有什么用?



返回列表 返回列表
评论

    分享到