Spring框架学习:JdbcTemplate的CURD(十八)

xml配置:

    <!--组件扫描-->
    <context:component-scan base-package="com.spring5"></context:component-scan>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql:///test" />
        <property name="username" value="root" />
        <property name="password" value="test" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <!--jdbcTemplate对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--注入dataSource-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

实体类:

public class User {

    private String userId;

    private String username;

    private String ustatus;

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getUstatus() {
        return ustatus;
    }

    public void setUstatus(String ustatus) {
        this.ustatus = ustatus;
    }

    @Override
    public String toString() {
        return "User{" +
                "userId='" + userId + '\'' +
                ", username='" + username + '\'' +
                ", ustatus='" + ustatus + '\'' +
                '}';
    }
}

service:

package com.spring5.service;

import com.spring5.dao.UserDao;
import com.spring5.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserService {

    //注入dao
    @Autowired
    private UserDao userDao;

    public void add(User user){
        userDao.add(user);
    }

    public void update(User user) {
        userDao.update(user);
    }
    public void delete(String id) {
        userDao.delete(id);
    }

    public int queryCount() {
        return userDao.queryCount();
    }

    public User queryOne(String id) {
        return userDao.queryOne(id);
    }

    public List<User> querList() {
        return userDao.querList();
    }

    public void batchAdd(List<Object[]> list){
        userDao.batchAdd(list);
    }
    public void batchUpdate(List<Object[]> list){
        userDao.batchUpdate(list);
    }

    public void batchDelete(List<Object[]> list){
        userDao.batchDelete(list);
    }
}

Dao:

public interface UserDao {

    void add(User user);

    void update(User user);

    void delete(String id);

    int queryCount();

    User queryOne(String id);

    List<User> querList();

    void batchAdd(List<Object[]> list);

    void batchUpdate(List<Object[]> list);

    void batchDelete(List<Object[]> list);
}

Dao实现类:

@Repository
public class UserDaoImpl implements UserDao{

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加的方法

    /**
     * update(String sql,Object...args)
     * 有两个参数
     * 第一个参数:sq1语句
     * 第二个参数:可变参数,设置sql语句值
     */
    @Override
    public void add(User user) {
        //创建sql语句
        String sql = "insert into user values (? , ? , ?)";
        Object[] args = {user.getUserId(),user.getUsername(),user.getUstatus()};
        int update = jdbcTemplate.update(sql, args);
        System.out.println("影响的行数:"+update);
    }

    @Override
    public void update(User user) {
        //创建sql语句
        String sql = "update user set username = ? , ustatus = ? where userId = ?";
        Object[] args = {user.getUsername(),user.getUstatus(),user.getUserId(),};
        int update = jdbcTemplate.update(sql, args);
        System.out.println("影响的行数:"+update);
    }

    @Override
    public void delete(String id) {
        String sql = "delete from user where userId = ?";
        int update = jdbcTemplate.update(sql, id);
        System.out.println("影响的行数:"+update);
    }

    /**
     * 2、使用JdbcTemplate实现查询返回某个值代码
     * queryForObject(String sql,Class<T>requiredType)
     * 有两个参数
     * 第一个参数:sq1语句
     * 第二个参数:返回类型Class
     */
    @Override
    public int queryCount() {
        String sql = "select count(*) from user";
        Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
        return count;
    }

    /**
     *queryForObject(String sgl,RowMapper<T>rowMapper,Object...args)
     * 有三个参数
     * 第一个参数:sq1语句
     * 第二个参数:RowMapper是接口,针对返回不同类型数据,使用这个接口里面实现类完成数据封装
     * 第三个参数:sq1语句值-参数值
     */
    @Override
    public User queryOne(String id) {
        String sql = "select * from user where userId = ?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);
        return user;
    }

    /**
     * query(String sql,RowMapper<T>rowMapper,Object...args)
     * 有三个参数:
     * 第一个参数:sq1语句
     * 第二个参数:RowMapper是接口,针对返回不同类型数据,使用这个接口里面实现类完成数据封装
     * 第三个参数:sq1语句值-参数值
     */
    @Override
    public List<User> querList() {
        String sql = "select * from user";
        List<User> list = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return list;
    }

    @Override
    public void batchAdd(List<Object[]> list) {
        String sql = "insert into user values (? , ? , ?)";
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        System.out.println("影响的行数:"+ Arrays.toString(ints));
    }

    @Override
    public void batchUpdate(List<Object[]> list) {
        String sql = "update user set username = ? , ustatus = ? where userId = ?";
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        System.out.println("影响的行数:"+ Arrays.toString(ints));
    }

    @Override
    public void batchDelete(List<Object[]> list) {
        String sql = "delete from user where userId = ?";
        int[] ints = jdbcTemplate.batchUpdate(sql, list);
        System.out.println("影响的行数:"+ Arrays.toString(ints));
    }
}

junit测试类:

    @Test
    public void testCurd(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        UserService userService = context.getBean("userService", UserService.class);

        //新增
//        User user = new User();
//        user.setUserId("3");
//        user.setUsername("Tom");
//        user.setUstatus("0");
//        userService.add(user);

        //修改
//        User user = new User();
//        user.setUserId("3");
//        user.setUsername("Tom2");
//        user.setUstatus("1");
//        userService.update(user);

        //删除
//        userService.delete("3");

        //查询-返回某一个值
        int count = userService.queryCount();
        System.out.println("count : "+count);

        //查询-对象
        User user = userService.queryOne("1");
        System.out.println(user);

        //查询-列表
        List<User> users = userService.querList();
        System.out.println(users);
    }

    @Test
    public void testBatchCurd(){

        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");

        UserService userService = context.getBean("userService", UserService.class);

        //批量新增
//        List<Object[]> list = new ArrayList<>();
//        Object[] o1 = {"11","T1","6"};
//        Object[] o2 = {"12","T2","7"};
//        Object[] o3 = {"13","T3","8"};
//        list.add(o1);
//        list.add(o2);
//        list.add(o3);
//        userService.batchAdd(list);

        //批量修改
//        List<Object[]> list = new ArrayList<>();
//        Object[] o1 = {"G1","16","11"};
//        Object[] o2 = {"G1","17","12"};
//        Object[] o3 = {"G1","18","13"};
//        list.add(o1);
//        list.add(o2);
//        list.add(o3);
//        userService.batchUpdate(list);
//
//        //删除
        List<Object[]> list = new ArrayList<>();
        Object[] o1 = {"11"};
        Object[] o2 = {"12"};
        list.add(o1);
        list.add(o2);
        userService.batchDelete(list);

    }
暂无评论

发送评论 编辑评论


				
上一篇
下一篇