1. 程式人生 > >springmvc-mybatis的增刪改查以及主鍵返回

springmvc-mybatis的增刪改查以及主鍵返回

基於原始dao開發,而不是mapper代理

1.usermapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">


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

    <!-- #{}表示一個佔位符 根據id查詢使用者-->
    <select
id="findUserById" parameterType="int" resultType="com.ssm.model.User">
SELECT * FROM USER WHERE id=#{value} </select> <!-- ${}表示拼接sql字串 根據姓名模糊查詢使用者 可能會造成sql注入為題,如果接收的引數為簡單型別,只能為${value} 與 #{}不一樣,可是使用任何名稱 --> <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> <!-- 非自增主鍵的返回 通過uuid() <selectKey keyProperty="id" order="before" resultType="java.lang.String"> SELECT uuid() </selectKey> 如果通過這個方式 那麼插入語句為 insert into user(id,username,password) values (#{id},#{username},#{password}) --> 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<User> 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<User> findUserByName(String username) {
        List<User> 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<User> 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";
    }


}

,以上