欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring增删改查

程序员文章站 2022-07-01 12:46:19
...

Spring增删改查

新建一个项目spring 新建的时候要勾选web
在web里新建一个lib包,导入以下jar包
Spring增删改查
新建以下包和JAVA文件
Spring增删改查
增删改查实现的代码如下
User中代码

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

    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 getPassword() {
        return password;
    }

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

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

UserDao中代码

@Component
public class UserDao implements IUserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public List<User> findAll() {
        String sql="select * from tb_user";
        BeanPropertyRowMapper<User> rowMapper=new BeanPropertyRowMapper<>(User.class);
        List<User> users=jdbcTemplate.query(sql,rowMapper);

        return users;

    }

    @Override
    public void delete(int id) {
        String sql="delete from tb_user where id=?";
        jdbcTemplate.update(sql,id);
    }

    @Override
    public void add(String username, String password) {
        String sql="insert into tb_user(username,password) values (?,?)";
        jdbcTemplate.update(sql,username,password);
    }

    @Override
    public void update(int id, String username, String password) {
        String sql="update tb_user set username=?,password=? where id=?";
        jdbcTemplate.update(sql,username,password,id);
    }
}

IUserDao中代码

public interface IUserDao {
    List<User> findAll();
    void delete(int id);
    void add(String username,String password);
    void update(int id,String username,String password);

}

UserService中代码

@Service
public class UserService implements IUserService {

    @Autowired
    private IUserDao userDao;

    @Override
    public List<User> findAll() {
        List<User> users=userDao.findAll();
        return users;
    }

    @Override
    public void delete(int id) {
        userDao.delete(id);
    }

    @Override
    public void add(String username, String password) {
        userDao.add(username,password);
    }

    @Override
    public void update(int id, String username, String password) {
        userDao.update(id,username,password);
    }
}

IUserService中

public interface IUserService {
    List<User> findAll();
    void delete(int id);
    void add(String username,String password);
    void update(int id,String username,String password);
}

Test 中代码

public class Test {
    public static void main(String[] args){

        ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
        IUserService userService=(IUserService) context.getBean("userService");
//        userService.findAll();
//        User user= (User) context.getBean("user");
//        System.out.println(user);

//        IUserDao userDao=(IUserDao) context.getBean("userDao");
//        List<User> users=userDao.findAll();
//        userDao.delete(4);
//        System.out.println(users);

//        IUserService userService= (IUserService) context.getBean("userService");
//        List<User> users1=userService.findAll();
//        System.out.println(users1);
//        userService.update(6,"aa","123");
//        List<User> users2=userService.findAll();
//        System.out.println(users2);

//        IUserService userService= (IUserService) context.getBean("userService");
        List<User> users=userService.findAll();
        System.out.println(users);
        userService.add("ccc","ccc");
        List<User> users1=userService.findAll();
        System.out.println(users1);
    }
}

spring.xml中代码

    <bean id="userService" class="com.zhongruna.service.Impl.UserService"></bean>
    <bean id="user" class="com.zhongruna.bean.User">
        <property name="id" value="1"></property>
        <property name="name" value="abc"></property>
        <property name="password" value="123"></property>
    </bean>
    
    <context:component-scan base-package="com.zhongruna"></context:component-scan>

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property value="jdbc:mysql://127.0.0.1:3306/zjgm?characterEncoding=utf-8" name="url"/>
        <property value="root" name="username"/>
        <property value="123456" name="password"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>