spring+mysql+druid+jdbctemplate使用xml配置实现增删改查

文章目录

  • 选型
  • domain层
  • 业务层
    • 接口
    • 实现类
  • 持久层
    • 接口
    • 实现类
  • beans.xml配置文件
  • 测试

选型

框架:spring
持久层:mysql+jdbcTemplate+druid
配置方式:beans.xml

domain层

package com.li.domain;

public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

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

业务层

接口

package com.li.service;

import com.li.domain.User;

import java.util.List;

public interface UserService {
    //得到全部用户名信息
    List<User> getUserAll();
    //根据id得到一名用户信息
    User getUserById(int id);
    //添加一名用户信息
    void InsertUser(User user);
    //修改用户信息
    void updateUser(User user);
    //删除用户信息
    void deleteUser(int id);
}

实现类

package com.li.service.impl;

import com.li.dao.UserDao;
import com.li.dao.impl.UserDaoImpl;
import com.li.domain.User;
import com.li.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao userDao;
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

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

    public User getUserById(int id) {
        return userDao.getUserById(id);
    }

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

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

    public void deleteUser(int id) {
        userDao.deleteUser(id);
    }
}

持久层

接口

package com.li.dao;

import com.li.domain.User;

import java.util.List;

public interface UserDao {
    //得到全部用户名信息
    List<User> getUserAll();
    //根据id得到一名用户信息
    User getUserById(int id);
    //添加一名用户信息
    void InsertUser(User user);
    //修改用户信息
    void updateUser(User user);
    //删除用户信息
    void deleteUser(int id);
}

实现类

package com.li.dao.impl;

import com.li.dao.UserDao;
import com.li.domain.User;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {
    //使用构造函数的方式进行注解
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    //private JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCTemplatesUtils.getDatasource());
    public List<User> getUserAll() {
        String sql = "select * from crud1";
        /*//将每一天记录封装为一个map集合,再将所有map集合添加到list
        List> maps = jdbcTemplate.queryForList(sql, new BeanPropertyRowMapper(User.class));*/
        List<User> users = jdbcTemplate.query(sql, new BeanPropertyRowMapper<User>(User.class));
        return users;
    }

    public User getUserById(int id) {
        String sql = "select * from crud1 where id=?";
        User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<User>(User.class), id);

        return user;
    }

    public void InsertUser(User user) {
        String sql = "insert into crud1(username,password) values(?,?)";
        int i = jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
        System.out.println(i);

    }

    public void updateUser(User user) {
        String sql = "update crud1 set username=? ,password=? where id=?";
        int i = jdbcTemplate.update(sql,user.getUsername() ,user.getPassword(), user.getId());
        System.out.println(i);
    }

    public void deleteUser(int id) {
        String sql = "delete from crud1 where id = ?";
        int i = jdbcTemplate.update(sql, id);
        System.out.println(i);

    }
}

beans.xml配置文件


<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.xsd">
    <bean id="userService" class="com.li.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao">property>
    bean>

    <bean id="userDao" class="com.li.dao.impl.UserDaoImpl">
    
        <property name="jdbcTemplate" ref="jdbcTemplate">property>
    bean>

    <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate" scope="prototype">
        
        <constructor-arg name="dataSource" ref="dataSource">constructor-arg>
    bean>

    
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver">property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false">property>
        <property name="username" value="root">property>
        <property name="password" value="root">property>
    bean>

beans>

测试

import com.li.domain.User;
import com.li.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
    UserService userService = applicationContext.getBean("userService", UserService.class);
    @org.junit.Test
    public void testFindall(){

        List<User> users = userService.getUserAll();
        for (User user : users) {
            System.out.println(user);
        }
    }
    @org.junit.Test
    public void testGetUserById(){
        User userById = userService.getUserById(1);
        System.out.println(userById);
    }

    @org.junit.Test
    public void testInsert(){
       User user = new User();
       user.setUsername("yaru");
       user.setPassword("yaru123");
       userService.InsertUser(user);

    }

    @org.junit.Test
    public void testUpdate(){
        User user = new User();
        user.setId(1);
        user.setUsername("admin");
        user.setPassword("admin");
        userService.updateUser(user);
    }

    @org.junit.Test
    public void testDelete(){
        userService.deleteUser(3);
    }
}

你可能感兴趣的:(spring+mysql+druid+jdbctemplate使用xml配置实现增删改查)