数据库的增删改查

实体类User.java
package com.zhu.vo;
/**
 * Created by idea on 2016/6/7.
 * 我就是你们的小星星
 */
public class User {
    private int id;
    private  String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    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 int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
数据访问层UserDao.java
package com.zhu.Dao;

import com.zhu.vo.User;

import java.util.List;

/**
 *Created by idea on 2016/6/7.
 * 我就是你们的小星星
 */
public interface UserDao {

    public int addUser(User user);

    public int deleteUser(String name);

    public int updateUser(User user);

    public ListselectUser(int id);

    public ListselectAll();
}

数据访问层实现类UserDaoImpl.java

package com.zhu.daoImpl;

import com.zhu.Dao.UserDao;
import com.zhu.vo.User;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *Created by idea on 2016/6/7.
 * 我就是你们的小星星
 */
@Repository("userDao")
public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
    @Autowired
    @Override
    public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory) {
        super.setSqlSessionFactory(sqlSessionFactory);
    }

    public int addUser(User user) {
        return getSqlSession().insert("mybatis/userMapper.addUser" , user);
    }

    public int deleteUser(String name) {
        return getSqlSession().delete("mybatis/userMapper.deleteUser" , name);
    }

    public int updateUser(User user) {
        return getSqlSession().update("mybatis/userMapper.updateUser" , user);
    }

    public List selectUser(int id) {
        return getSqlSession().selectList("mybatis/userMapper.selectUser" , id);
    }

    public List selectAll() {
        return getSqlSession().selectList("mybatis/userMapper.selectAll");
    }
}
业务层UserService.java
package com.zhu.service;

import com.zhu.vo.User;

import java.util.List;
import java.util.jar.Attributes;

/**
 *Created by idea on 2016/6/7.
 * 我就是你们的小星星
 */
public interface UserService {

    public int addUser(User user);

    public int deleteUser(String name);

    public int updateUser(User user);

    public ListselectUser(int id);

    public ListselectAll();

}
业务层实现类UserServiceImpl.java
package com.zhu.serviceImpl;

import com.zhu.Dao.UserDao;
import com.zhu.service.UserService;
import com.zhu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *Created by idea on 2016/6/7.
 * 我就是你们的小星星
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public int addUser(User user) {
        return userDao.addUser(user);
    }

    public int deleteUser(String name) {
        return userDao.deleteUser(name);
    }

    public int updateUser(User user) {
        return userDao.updateUser(user);
    }

    public List selectUser(int id) {
        return userDao.selectUser(id);
    }

    public List selectAll() {
        return userDao.selectAll();
    }
}
处理层UserController.java
package com.zhu.controller;

import com.zhu.service.UserService;
import com.zhu.vo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**
 *Created by idea on 2016/6/7.
 * 我就是你们的小星星
 */
@Controller
@RequestMapping("/handle")
public class UserController {

    @Autowired
    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public List add(User user){
        List list = new ArrayList();
        list.add(userService.addUser(user));
        return list;
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public List delete(@RequestParam("name") String name){
        System.out.println(name);
        List list = new ArrayList();
        list.add(userService.deleteUser(name));
        return list;
    }

    @RequestMapping("/updateUser")
    @ResponseBody
    public List update(User user){
        List list = new ArrayList();
        list.add(userService.updateUser(user));
        return list;
    }

    @RequestMapping("/selectUser")
    @ResponseBody
    public List selectUser(@RequestParam("id") int id){
        List list = new ArrayList();
        list.addAll(userService.selectUser(id));
        return list;
    }

    @RequestMapping("/selectAll")
    @ResponseBody
    public List selectAll(){
        List list = new ArrayList();
        list.addAll(userService.selectAll());
        return list;
    }
}
配置文件web.xml



  Archetype Created Web Application
    
    
        contextConfigLocation
        classpath:spring/beans.xml
    
    
        org.springframework.web.context.ContextLoaderListener
    

    
        characterEncodingFilter
        org.springframework.web.filter.CharacterEncodingFilter
        
            encoding
            UTF-8
        
    
    
        characterEncodingFilter
        /*
    

    
        springController
        org.springframework.web.servlet.DispatcherServlet
        
            contextConfigLocation
            classpath:springmvc/spring-controller.xml
        
        1
    
    
        springController
        *.do
    

配置文件beans.xml(spring配置)



    
       
    

    
        
        
        
        
    

    
        
        
    

    
        
    
    
        
            
        
    
    
        
        
    

    
资源配置文件db.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis
username=root
password=root
配置文件mybatis-config.xml




    
        
    
    
        
    

配置文件userMapper.xml



    

        
            insert into user (name , age ) values (#{name},#{age})
        

        
            delete from user where name = #{name}
        

        
            update user set name = #{name} , age = #{age} where id = #{id}
        

        

        
    
配置文件spring-controller(springmvc



    

    

    
        
            
                text/html;charset=UTF-8
                text/plain;charset=UTF-8
            
        
    
    
    
        
            
                
                
            
        
    

user.jsp页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>


    Title
    
    
    


    
   
   
     
       


编号 姓名 年龄
因时间紧迫博文中没有加以注释,如果博友有问题请提出(本人也是初出茅庐)大家一起学习,共同进步。



你可能感兴趣的:(原创)