基于Spring框架的管理系统(一)

一、

基于Spring框架的管理系统(一)_第1张图片
基于Spring框架的管理系统(一)_第2张图片

二、在web中创建一个lib包,将以下架包导入

commons-logging-1.2.jar
log4j-1.2.17.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
mysql-connector-java-5.1.8.jar
spring-aop-4.3.6.RELEASE.jar
spring-aspects-4.3.6.RELEASE.jar
spring-beans-4.3.6.RELEASE.jar
spring-context-4.3.6.RELEASE.jar
spring-expression-4.3.6.RELEASE.jar
spring-core-4.3.6.RELEASE.jar
spring-jdbc-4.3.6.RELEASE.jar
spring-tx-4.3.6.RELEASE.jar
基于Spring框架的管理系统(一)_第3张图片

三、在src下新建com.zhong包,再在zhong中创建如下包:

bean,dao, service, servlet, test, util

基于Spring框架的管理系统(一)_第4张图片

1.在bean包下New一个User

package com.zhongruan.bean;

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 + '\'' +
                '}';
    }
}

2.在dao包下New一个IUserDao接口如下:

package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

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


}

3.在dao包下新建impl,在impl下New一个UserDao:

package com.zhongruan.dao.impl;

import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;


@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 insert( 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);
    }
}

4.在dao包下New一个IUserService接口如下:

package com.zhongruan.service;

import com.zhongruan.bean.User;

import java.util.List;

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

5.在service包下新建impl,在impl下New一个UserService:

package com.zhongruan.service.impl;

import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@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 update(int id,String username,String password) {
        userDao.update(id,username,password);
        System.out.println("update");
    }

    @Override
    public void insert(String username, String password) {
        userDao.insert(username,password);
        System.out.println("insert");
    }


    @Override
    public void delete(int id) {
        userDao.delete(id);
        System.out.println("delete");
    }
}

6.在test下Nem一个Test.java:

package com.zhongruan.test;

import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

public class Test {
    public static void main(String[] args){
       ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
       IUserService userService=(IUserService) context.getBean("userService");
        List<User>users=userService.findAll();
        System.out.println(users);
        userService.delete(2);
        userService.insert("sisu","0416");
        userService.update(3,"xlb","6");

    }
}

spring.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- <bean id="userService" class="com.zhongruan.service.impl.UserService"></bean>-->

    <bean id="user" class="com.zhongruan.bean.User">
        <property name="id" value="1"></property>
        <property name="username" value="sisu"></property>
        <property name="password" value="123"></property>
    </bean>


    <context:component-scan base-package="com.zhongruan"></context:component-scan>


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


    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


</beans>

你可能感兴趣的:(基于Spring框架的管理系统(一))