package org.learn.spring5.dao.impl;
import org.learn.spring5.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
public class UserDaoImpl implements UserDao{
//注入JdbcTemplate對象
@Autowired
private JdbcTemplate jdbcTemplate;
}
编写实体类User
package org.learn.spring5.entiy;
public class User {
//id
private Integer id;
//用户名
private String userName;
//用户状态
private Integer status;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
}
创建数据和表结构
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
`id` int(11) NOT NULL,
`user_name` varchar(22) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`status` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
SET FOREIGN_KEY_CHECKS = 1;
具体操作
JdbcTemplate操作数据库-添加功能
1)在UserDaoImpl类里添加add方法
2)调用JdbcTemplate对象的update方法
package org.learn.spring5.dao.impl;
import org.learn.spring5.entiy.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
@Component
public class UserDaoImpl implements UserDao{
//注入JdbcTemplate對象
@Autowired
private JdbcTemplate jdbcTemplate;
public void add(User user) {
//1.創建sql語句
String sql="insert into t_user values(?,?,?)";
//2.調用方法实现
Object[] args={user.getId(),user.getUserName(),user.getStatus()};
int update = jdbcTemplate.update(sql, args);
System.out.println(update);
}
}
3)编写测试程序
import org.junit.Test;
import org.learn.spring5.entiy.User;
import org.learn.spring5.service.impl.UserService;
import org.learn.spring5.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpring5 {
/**
* 操作JdbcTemplate,添加操作
*/
@Test
public void testAdd() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
User user = new User();
user.setId(1);
user.setUserName("张三");
user.setStatus(1);
userService.addUser(user);
}
}
程序返回结果1,标识数据添加成功!
JdbcTemplate操作数据库-修改功能
public int update(User user) {
String sql = "update t_user set user_name =?,status=? where id =?";
Object[] args = {user.getUserName(), user.getStatus(), user.getId()};
int update = jdbcTemplate.update(sql, args);
return update;
}
编写测试程序
//JdbcTemplate修改操作
@Test
public void testUpdate() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
User user = new User();
user.setId(1);
user.setUserName("张四");
user.setStatus(0);
userService.update(user);
}
JdbcTemplate操作数据库-查询功能
1)查询返回某一个值
public int queryCount() {
String sql = "select count(*) from t_user";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
return count;
}
编写测试程序
//JdbcTemplate 查询操作,返回用户的数量
@Test
public void testQueryCount() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
int count = userService.queryCount();
System.out.println(count);
}
2)查询返回对象
public User findOne(Integer id) {
String sql = "select * from t_user where id =?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(User.class), id);
return user;
}
编写测试程序
//JdbcTemplate 查询操作,返回对象根据id
@Test
public void testQueryById() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
User user = userService.queryById(1);
System.out.println(user);
}
3)查询返回集合
//JdbcTemplate 查询操作,返回集合对象
@Test
public void testFindAll() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
List all = userService.findAll();
System.out.println(all);
}
编写测试程序
//JdbcTemplate 查询操作,返回集合对象
@Test
public void testFindAll() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
List all = userService.findAll();
System.out.println(all);
}
JdbcTemplate操作数据库-删除功能
public int del(Integer id) {
String sql = "delete from t_user where id =?";
int update = jdbcTemplate.update(sql, id);
return update;
}
编写测试程序
//JdbcTemplate删除操作
@Test
public void testDel() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
userService.del(1);
}
JdbcTemplate操作数据库-批量添加
public void batchAdd(List
编写测试程序
//JdbcTemplate 批量添加操作
@Test
public void batchAdd() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
List
JdbcTemplate操作数据库-批量修改和删除
//批量修改操作
public void batchUpdate(List users) {
String sql = "update t_user set user_name =?,status=? where id =?";
int[] ints = jdbcTemplate.batchUpdate(sql, users);
System.out.println(Arrays.asList(ints).toString());
}
//批量删除操作
public void batchDel(List users) {
String sql = "delete from t_user where id =?";
int[] ints = jdbcTemplate.batchUpdate(sql, users);
System.out.println(ints);
}
编写测试程序
//JdbcTemplate 批量修改操作
@Test
public void batchUpdate() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
List list = new ArrayList();
Object[] o1 = {"namexiugai", 1, 4};
Object[] o2 = {"name2xiugai", 1, 5};
Object[] o3 = {"name3xiugai", 1, 6};
list.add(o1);
list.add(o2);
list.add(o3);
userService.batchUpdate(list);
}
//JdbcTemplate 批量删除操作
@Test
public void batchDel() {
ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
UserService userService = context.getBean("userServiceImpl", UserServiceImpl.class);
List list = new ArrayList();
Object[] o1 = {4};
Object[] o2 = {5};
Object[] o3 = {6};
list.add(o1);
list.add(o2);
list.add(o3);
userService.batchDel(list);
}
Given a sorted linked list, delete all duplicates such that each element appear only once.
For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return&
在JDK1.5之前的单例实现方式有两种(懒汉式和饿汉式并无设计上的区别故看做一种),两者同是私有构
造器,导出静态成员变量,以便调用者访问。
第一种
package singleton;
public class Singleton {
//导出全局成员
public final static Singleton INSTANCE = new S