//User实体类
package com.tao.pojo; public class User { private int id;
private String name;
private String gender; public User() {
super();
}
public User(int id, String name, String gender) {
super();
this.id = id;
this.name = name;
this.gender = gender;
}
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", gender=" + gender + "]";
} } //Userdao方法 package com.tao.dao; import java.util.List; import com.tao.pojo.User; public interface UserDAO { //添加数据库
public boolean save(User user);
//更新数据
public boolean update(User user);
//根据ID删除数据
public boolean deleteById(int id);
//查询所有
public List<User> QueryAll();
//根据ID查询
public User QueryById(int id); } //UserDAOImpl实现类 package com.tao.impl; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import javax.sql.DataSource; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import com.tao.dao.UserDAO;
import com.tao.pojo.User; public class UserDAOImpl implements UserDAO{
//生成Template的set方法
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} // 返回结果与实体类类做对应用的
RowMapper<User> mappUser=new RowMapper<User>() { @Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
// TODO Auto-generated method stub
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setGender(rs.getString("gender"));
return user;
}
}; @Override
public boolean save(User user) {
// TODO Auto-generated method stub
int rows = jdbcTemplate.update("insert into user values(?,?,?)", user.getId(),user.getName(),user.getGender());
if (rows>0) {
return true;
}
return false;
} @Override
public boolean update(User user) {
// TODO Auto-generated method stub
int rows = jdbcTemplate.update("update user set name=?,gender=? where id=?",user.getName(),user.getGender(),user.getId());
if(rows>0) {
return true;
}
return false;
}
//根据ID删除
@Override
public boolean deleteById(int id) {
// TODO Auto-generated method stub
int rows = jdbcTemplate.update("delete from user where id=?", id);
if (rows>0) {
return true;
}
return false;
} @Override
public List<User> QueryAll() {
// TODO Auto-generated method stub
List<Map<String,Object>> listMap = jdbcTemplate.queryForList("select * from user");
ArrayList<User> list = new ArrayList<User>();
for(int i=0;i<listMap.size();i++) {
User user = new User();
Map<String, Object> map = listMap.get(i);
user.setId((int) map.get("id"));
user.setName((String) map.get("name"));
user.setGender((String) map.get("gender"));
list.add(user);
}
return list;
}
//根据ID查询
@Override
public User QueryById(int id) {
// TODO Auto-generated method stub
User user = jdbcTemplate.queryForObject("select * from user where id=?", new Object[] {id}, mappUser);
return user;
}
//统计人数
public int GetCount() {
Integer count = jdbcTemplate.queryForObject("select count(id) from user", Integer.class);
return count;
} } //applicationContext.xm文件在src目录下 <?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-4.3.xsd"> <!-- 数据源 -->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSources">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/test0116_spring?characterEncoding=utf-8"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!-- 将 DataSource 注入Template -->
<!-- 单独配置JDBCTemplate的bean ,ref是指将数据源注给Template-->
<bean class="org.springframework.jdbc.core.JdbcTemplate" id="jdbcTemplate">
<property name="dataSource" ref="dataSources"></property>
</bean>
<!-- ref 引用的是Template的 -->
<bean class="com.tao.impl.UserDAOImpl" id="UserDAO">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> </beans> //test 测试类 package com.tao.test; import java.util.List; import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.tao.impl.UserDAOImpl;
import com.tao.pojo.User; public class TestUser {
UserDAOImpl users ;
@Before
public void TestB() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
users = context.getBean("UserDAO", UserDAOImpl.class);
} //保存
@Test
public void testSave() {
User user = new User(6, "可可", "男");
boolean b = users.save(user);
System.out.println(b);
} //更新数据
@Test
public void TestUpdate() {
User user = new User(1, "vv", "女");
boolean b = users.update(user);
System.out.println(b); }
//根据ID删除数据
@Test
public void TestDelete() {
boolean b = users.deleteById(6);
System.out.println(b);
}
//根据ID查询数据
@Test
public void TestQueryID() {
User user = users.QueryById(5);
System.out.println(user); }
//查询所有
@Test
public void TestQueryAll() {
List<User> list = users.QueryAll();
for(User uu:list) {
System.out.println(uu);
}
}
//统计人数
@Test
public void testCount() {
int count = users.GetCount();
System.out.println(count); } } 所需jar包如图所示(必须的6个,再加一个 logging,数据库的一个jdbc,事物的一个jdbc,)