[Spring JDBC] Database development: basic operations (addition, deletion, modification)



Database development: basic operations (add, delete, modify)

① Preparation

  • 1. Introduce the relevant jar package:
    Insert picture description here

  • 2. Configure the database connection pool in the Spring configuration file:
    Insert picture description here

  • 3. Configure the JdbcTemplate object and inject the DataSource
    Insert picture description here

  • 4. Create service class, create dao class, and inject jdbcTemplate object in dao
    Insert picture description here

Back to top


② Database operation-add [insert] update()

1. Create an entity class corresponding to the database

  • Create database tablet_user
    Insert picture description here
  • In User类, we set t_user表each field as the attribute of the class to facilitate the assignment and acquisition of values ​​in subsequent operations.
package JDBC.entry;

public class User {
    
    

    // 创建字段对应属性
    private String useId;
    private String userName;
    private String uststus;

    // set\get方法
    public String getUseId() {
    
    
        return useId;
    }

    public void setUseId(String useId) {
    
    
        this.useId = useId;
    }

    public String getUserName() {
    
    
        return userName;
    }

    public void setUserName(String userName) {
    
    
        this.userName = userName;
    }

    public String getUststus() {
    
    
        return uststus;
    }

    public void setUststus(String uststus) {
    
    
        this.uststus = uststus;
    }
}

2. Write service and dao

  • (1) Add database operation in dao (interface implementation class rewrite specific implementation)

  • (2) when the specific implementation, which calls the object JdbcTemplate update()implemented method of operation add
    Insert picture description here

    • The first parameter:sql 语句
    • The second parameter: variable parameters, settings sql 语句值
  • (3) SQL add statement:insert into 表名 values (value1,value2,value3)

@Repository
public class UserDaoImpl implements UserDao {
    
    

    // 注入JdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 重写add方法
    @Override
    public void add(User user) {
    
    
        // sql语句
        String sql = "insert into t_user values(?,?,?)";
        // 可变数组保存sql语句插入值
        Object[] args = {
    
    user.getUseId(), user.getUserName(), user.getUststus()};
        // 调用方法实现数据库信息插入
        int update = jdbcTemplate.update(sql,args);
        System.out.println("插入操作影响的记录数:"+ update);

    }
}
  • Process:
    Insert picture description here

3. Testing

  • In the test class, userService对象after obtaining , call the addUser()method to pass in the user object. Before that, first create a user object, and user.setXxx()assign values ​​to each field through methods.
@org.junit.Test
public void test_jdbc(){
    
    
    try {
    
    
        // 1.获取配置文件对象
        ApplicationContext context = new ClassPathXmlApplicationContext("JDBC/bean_jdbc.xml");
        UserService userService = context.getBean("userService",UserService.class);
        // 创建user对象,设置属性
        User user = new User();
        // user.setUseId("1");
        user.setUseId("2");
        user.setUserName("Vae");
        // user.setUststus("creating");
        user.setUststus("singing");
        // 调用方法
        userService.addUser(user);
    } catch (Exception e){
    
    
        e.printStackTrace();
    }
}

Insert picture description here
Insert picture description here
Back to top


③ Database operation-delete [delete] update()

1. Write service and dao

  • The deletion of the record can be done through the record id, so the deleteUser()parameter passed in is the record to be deleted id.
  • SQL delete statement:delete from 表名 where 字段1=value1
public interface UserDao {
    
    
    // 添加用户的方法
    void add(User user);
    // 修改用户的方法
    void updateUser(User user);
    // 删除用户记录
    void deleteUser(String id);
}
@Service
public class UserService {
    
    
    // 注入UserDao对象属性
    @Autowired
    private UserDao userDao;

    // 添加用户
    public void addUser(User user){
    
    
        userDao.add(user);
    }

    // 修改用户
    public void updateUser(User user){
    
    
        userDao.updateUser(user);
    }
    // 删除用户
    public void deleteUser(String id){
    
    
        userDao.deleteUser(id);
    }
}

2. Write an implementation class to specifically implement database record deletion

@Repository
public class UserDaoImpl implements UserDao {
    
    

    // 注入JdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 重写add方法
    @Override
    public void add(User user) {
    
    
        // sql语句
        String sql = "insert into t_user values(?,?,?)";
        // 可变数组保存插入值
        Object[] args = {
    
    user.getUseId(), user.getUserName(), user.getUststus()};
        // 调用方法实现数据库信息插入
        int insert = jdbcTemplate.update(sql,args);
        System.out.println("插入操作影响的记录数:"+ insert);

    }
    // 重写updateuser方法
    @Override
    public void updateUser(User user) {
    
    
        // sql语句
        String sql = "update t_user set username=?,ustatus=? where user_id=?";
        // 可变数组保存修改值
        Object[] args = {
    
    user.getUserName(),user.getUststus(),user.getUseId()};
        // 调用方法实现数据库信息修改
        int update = jdbcTemplate.update(sql,args);
        System.out.println("插入操作影响的记录数:"+ update);
    }
    // 删除用户记录
    @Override
    public void deleteUser(String id) {
    
    
        // sql语句
        String sql = "delete from t_user where user_id=?";
        // 调用方法实现数据库信息删除
        int delete = jdbcTemplate.update(sql,id;
        System.out.println("插入操作影响的记录数:"+ delete);
    }
}

3. Test class

@org.junit.Test
public void test_jdbc(){
    
    
    try {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("JDBC/bean_jdbc.xml");
        UserService userService = context.getBean("userService",UserService.class);
        // 创建user对象,设置属性
        User user = new User();
        user.setUseId("3");
        user.setUserName("Vae");
        user.setUststus("dancing");
        // 调用方法增加用户
        userService.addUser(user);
        // 调用方法删除用户
        userService.deleteUser("2");
    } catch (Exception e){
    
    
        e.printStackTrace();
    }
}

Insert picture description here

Here we first insert a record, and then delete the record with id 2 in the table, so now the remaining records in the table are 1, 3 records.
Insert picture description here

Back to top


④ Database operation-change [update] update()

1. Write service and dao

  • When realizing user information changes, we also adopt the strategy of modifying through id.
  • SQL modification statement:update 表名 set 字段1=value1,字段2=value2 where 字段3=value3
public interface UserDao {
    
    
    // 添加用户的方法
    void add(User user);
    // 修改用户的方法
    void updateUser(User user);
}
@Service
public class UserService {
    
    
    // 注入UserDao对象属性
    @Autowired
    private UserDao userDao;

    // 添加用户
    public void addUser(User user){
    
    
        userDao.add(user);
    }

    // 修改用户
    public void updateUser(User user){
    
    
        userDao.updateUser(user);
    }
}

2. Write the implementation class to implement the database modification

@Repository
public class UserDaoImpl implements UserDao {
    
    

    // 注入JdbcTemplate对象
    @Autowired
    private JdbcTemplate jdbcTemplate;

    // 重写add方法
    @Override
    public void add(User user) {
    
    
        // sql语句
        String sql = "insert into t_user values(?,?,?)";
        // 可变数组保存插入值
        Object[] args = {
    
    user.getUseId(), user.getUserName(), user.getUststus()};
        // 调用方法实现数据库信息插入
        int insert = jdbcTemplate.update(sql,args);
        System.out.println("插入操作影响的记录数:"+ insert);

    }
    // 重写updateuser方法
    @Override
    public void updateUser(User user) {
    
    
        // sql语句
        String sql = "update t_user set username=?,ustatus=? where user_id=?";
        Object[] args = {
    
    user.getUserName(),user.getUststus(),user.getUseId()};
        // 调用方法实现数据库信息修改
        int update = jdbcTemplate.update(sql,args);
        System.out.println("插入操作影响的记录数:"+ update);
    }
}

3. Test class

@org.junit.Test
public void test_jdbc(){
    
    
    try {
    
    
        ApplicationContext context = new ClassPathXmlApplicationContext("JDBC/bean_jdbc.xml");
        UserService userService = context.getBean("userService",UserService.class);
        // 创建user对象,设置属性
        User user = new User();
        // 调用方法修改用户
        user.setUseId("3"); // 根据id进行修改
        user.setUserName("VAE");
        user.setUststus("singing");
        userService.updateUser(user);
    } catch (Exception e){
    
    
        e.printStackTrace();
    }
}

Insert picture description here
Modify its user information by id=3: usernamechange 大写, statuschange singing.
Insert picture description here

Back to top


Note: The
database entity class 1. The article needs to be corrected: the corresponding field properties to be re-named userId, , userName(whichuserStatus are also used in the query needs to be changed).
2. The table t_user designed in the database also needs to be changed to:
Insert picture description here
see my blog: jdbcTemplate.queryXxx() The return value is null

Guess you like

Origin blog.csdn.net/qq_45797116/article/details/114295928