发表于: 2019-12-13 17:09:04
1 1279
今天完成的事情:
1.使用JDBCTemplate实现Interface和Imple的分离
目录如下
1)get、set方法(User.java)
package com.wp.bean;
public class User {
private int id;
private String name;
private String pwd;
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 String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
2)接口(UserService.java)
package com.wp.service;
import com.wp.bean.User;
public interface UserService {
//添加用户
void addUser(User user);
//由Id号删除用户
void deleteUserById(User user);
//由Id号修改用户
void updateUserById(User user);
//由Id号查询用户
String searchUserNameById(int id);
}
3)接口实现类(UserServiceImpl.java)
package com.wp.impl;
import com.wp.bean.User;
import com.wp.service.UserService;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class UserServiceImpl extends JdbcDaoSupport implements UserService {
@Override
public void addUser(User user) {
this.getJdbcTemplate().update("insert into user(name, pwd) values(?, ?)", user.getName(), user.getPwd());
}
@Override
public void deleteUserById(User user) {
this.getJdbcTemplate().update("delete from user where id = ?", user.getId());
}
@Override
public void updateUserById(User user) {
this.getJdbcTemplate().update("update user set name = ? where id = ?", user.getName(), user.getId());
}
@Override
public String searchUserNameById(int id) {// 简单查询,按照ID查询,返回字符串
String sql = "select name from user where id=?";
// 返回类型为String(String.class)
return this.getJdbcTemplate().queryForObject(sql, String.class, id);
}
}
4)测试类(ConnectionTest.java)
package com.wp.testconn;
import com.wp.bean.User;
import com.wp.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ConnectionTest {
@Test
public void testAdd() {
User user = new User();
user.setName("轻轻");
user.setPwd("123456");
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService dao = (UserService) applicationContext.getBean("UserService");
dao.addUser(user);
System.out.println("success!");
}
@Test
public void testDelete() {
User user = new User();
user.setName("admin");
user.setId(8);
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService dao = (UserService) applicationContext.getBean("UserService");
dao.deleteUserById(user);
System.out.println("success!");
}
@Test
public void testUpdate() {
User user = new User();
user.setName("刚刚");
user.setId(7);
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService dao = (UserService) applicationContext.getBean("UserService");
dao.updateUserById(user);
System.out.println("success!");
}
@Test
public void testSearchUserNameById() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService dao = (UserService) applicationContext.getBean("UserService");
String name = dao.searchUserNameById(1);
System.out.println(name);
}
}
5)在applicationContext.xml中添加如下代码
<bean id="UserService" class="com.wp.impl.UserServiceImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
2.简单学习了一下Spring
明天计划的事情:学学mybatis
遇到的问题:
1.第一次找的是SpringBoot的教程,写完之后看着简单但是无法运行,里面用的注解我也没见过,估计还是少配置了一些没学过的东西,听师兄的又找了个Spring的教程,里面用了继承JdbcDaoSupport类,把这个继承的类去掉了,配置文件就报错。
于是我就搜了一下这个类解释如下
通过扩展(继承)JdbcDaoSupport,不再需要在类中设置数据源和JdbcTemplate,您只需要将正确的数据源注入JdbcCustomerDAO。然后,您可以使用getJdbcTemplate()方法获取JdbcTemplate。
2.在实现类中查询的方法里要返回一个name,返回值类型是返回类型为String,没理解为什么要写String.class,
为什么return this.getJdbcTemplate().queryForObject(sql, String.class, id);的返回值就是String类型的。
百度上搜了一下也没找到具体解释。
收获:
1.get和set方法
set设置对象属性值,get从对象属性中获取值。get是取值 set 是设置值 。这是 java的 面向对象编程的一个特点。JAVA中有一个概念叫做封装,封装就是将对象一些自身的属性封装起来,隐藏起来,不让外界任意访问,如果你要让外界访问这个属性你就要提供get和set方法。get和set方法不是一定要必须两者都有,可以根据实际情况而定。
2.Spring
6)灵活的MVC Web应用框架。
3. SpringMVC核心组件
1)DispatcherServlet
前端控制器,主要职责是接收所有请求(根据配置文件来决定),并将请求转发给对应的控制器,接收控制器的处理结果,确定最终由哪个视图完成响应!
2)HandlerMapping
处理请求路径与控制器的映射关系。
3)Controller
实际处理请求的组件,例如接收请求参数,决定最终是转发或重定向的方式来响应。
4)ModelAndView
控制器的处理结果,其中的Model表示转发的数据(如果是重定向,则Model没有意义),而View表示最终负责响应的视图组件的名称。
5)ViewResolver
根据视图组件的名称,确定具体使用的是哪个视图组件。
评论