springmvc-mybatis의 삭제 수정 및 메인 키 반환

12172 단어

마퍼 에이전트가 아닌 원시dao 개발 기반


1.usermapper.xml




<mapper namespace="cn.itcast.ssm.mapper.UserMapper">

    
    <select id="findUserById" parameterType="int" resultType="com.ssm.model.User">
        SELECT * FROM USER WHERE id=#{value}
    select>

    
    <select id="findUserByName" parameterType="java.lang.String" resultType="com.ssm.model.User">
        SELECT * FROM USER WHERE USERNAME LIKE '%${value}%'
    select>

    
    <insert id="insertUser" parameterType="com.ssm.model.User">
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            SELECT LAST_INSERT_ID()
        selectKey>
        
        insert into user(username,password) values (#{username},#{password})
    insert>


    
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    delete>

    
    <update id="updateUser" parameterType="com.ssm.model.User">
        update user set username=#{username},password=#{password} where id=#{id}
    update>
mapper>

2.userdao.java
package com.ssm.dao;

import java.util.List;

import com.ssm.model.User;

public interface UserDAO {

    public User selectByID(int id);
    public List findUserByName(String username);
    public void insertUser(User user);
    public void deleteUser(int id);
    public void updateUser(User user);
}

//dao  

package com.ssm.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import com.ssm.dao.UserDAO;
import com.ssm.model.User;

public class UserDAOImpl extends SqlSessionDaoSupport implements UserDAO {

    @Override
    public User selectByID(int id) {
        SqlSession sqlsession = this.getSqlSession();

        User user = sqlsession.selectOne("findUserById", id);
        return user;
    }

    @Override
    public List findUserByName(String username) {
        List list = null;
        list = this.getSqlSession().selectList("findUserByName", username);
        return list;
    }

    @Override
    public void insertUser(User user) {
        this.getSqlSession().insert("insertUser", user);
    }

    @Override
    public void deleteUser(int id) {
        this.getSqlSession().delete("deleteUser", id);
    }

    @Override
    public void updateUser(User user) {
        this.getSqlSession().update("updateUser", user);
    }


}

3.springmvc 컨트롤러
package com.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

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.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.ssm.dao.UserDAO;
import com.ssm.model.User;


@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserDAO dao;

    @RequestMapping(value="/select",method=RequestMethod.GET)
    public String selectById(@RequestParam("id")int id)
    {
        System.out.println("test");
        User user = dao.selectByID(id);
        System.out.println(user.toString());
        return "index";
    }

    @RequestMapping(value="/selectbyname",method=RequestMethod.GET)
    public String findUserByName(@RequestParam("username")String username,HttpServletRequest request)
    {
        List list = dao.findUserByName(username);
        request.setAttribute("userlist", list);

        return "user";
    }

    //  ,    User  
    @RequestMapping(value="/insert",method=RequestMethod.GET)
    public String insertUser()
    {
        User user = new User();
        user.setPassword("insert");
        user.setUsername("insert");
        dao.insertUser(user);
        System.out.println(user.getId());
        return "index";
    }

    @RequestMapping(value="/delete",method=RequestMethod.GET)
    public String deleteUser(@RequestParam("id")int id)
    {
        dao.deleteUser(id);
        return "index";
    }
    @RequestMapping(value="/update",method=RequestMethod.GET)
    public String updateUser()
    {
        User user = new User();
        user.setId(8);
        user.setPassword("insertAfter");
        user.setUsername("insertAfter");
        dao.updateUser(user);
        return "index";
    }


}


이상

좋은 웹페이지 즐겨찾기