springboot框架的搭建(5)--》mybatis xml映射方式的增删改查
现在来用生成的代码进行增删改查

因为我后来把生成的mapper放到了上面去了 所以要在porm文件中添加一些字让他可以扫描到下面的xml文件
<!-- idea 扫描dao下xml与接口 不然默认不扫描同一包下xml文件 eclise不用设置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
accountMapper.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="com.lin.durid1.Dao.accountDao"> <sql id="Base_Column_List" > money </sql> <resultMap id="BaseResult" type="com.lin.durid1.enti.account"> <id column="peopleid" jdbcType="INTEGER" property="peopleid" /> <result column="money" jdbcType="BIGINT" property="money" /> <result column="vip" jdbcType="INTEGER" property="vip" /> </resultMap> <select id="selectAll" resultMap="BaseResult"> select * from account </select> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResult"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. This element was generated on Tue Dec 29 16:33:26 CST 2020. --> select <include refid="Base_Column_List" /> from account where peopleid = #{peopleid,jdbcType=INTEGER} </select> <insert id="insertSelective" parameterType="com.lin.enti.account"> insert into account <trim prefix="(" suffix=")" suffixOverrides=","> <if test="peopleid != null"> peopleid, </if> <if test="money != null"> money, </if> <if test="vip != null"> vip, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="peopleid != null"> #{peopleid,jdbcType=INTEGER}, </if> <if test="money != null"> #{money,jdbcType=BIGINT}, </if> <if test="vip != null"> #{vip,jdbcType=INTEGER}, </if> </trim> </insert> <!-- 自动编号(自增)的列在代码中无需显式地增加或修改操作,增加时数据库系统会自动插入值,并且不能修改其值--> <update id="updateSelective" parameterType="com.lin.enti.account"> update account <set> <if test="money != null"> money = #{money,jdbcType=BIGINT}, </if> <if test="vip != null"> vip = #{vip,jdbcType=INTEGER}, </if> </set> where peopleid = #{peopleid,jdbcType=INTEGER} </update> <delete id="delete" parameterType="java.lang.Integer"> delete from account where peopleid = #{peopleid,jdbcType=INTEGER} </delete> </mapper>
accountDao.class
package com.lin.durid1.Dao; import com.lin.durid1.enti.account; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface accountDao { List<account> selectAll(); List<account> selectByPrimaryKey(int peopleid); void insertSelective(account account); void updateSelective(account account); void delete(int peopeleid); }
controller直接调用 业务层先不写
package com.lin.durid1.Controller;
import com.lin.durid1.Dao.accountDao;
import com.lin.durid1.Dao.accountMapper;
import com.lin.durid1.enti.account;
import com.lin.durid1.enti.people;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.List;
@RestController
@RequestMapping("/test")
public class UserController {
@Resource
DataSource dataSource;
/*
@Resource
accountMapper accountMapper;
*/
@Resource
accountDao accountDao;
@GetMapping("/w")
public void show() throws SQLException {
System.out.println(dataSource.getConnection());
}
@GetMapping("/hello")
public String hello(){
return "hellotheganster";
}
@GetMapping("/mybatis")
public List<account> find(@RequestParam(value = "id")int id){
return accountDao.selectByPrimaryKey(id);}
//curd在下面4个
@GetMapping("/my")
public List<account> selectAll(){
return accountDao.selectAll();
}
@GetMapping("/in")
public void inAll(account account){
accountDao.insertSelective(account);
}
@GetMapping("/up")
public void updateSelective(account account){
accountDao.updateSelective(account);
}
@GetMapping("/de")
public void delete(@RequestParam(value ="id")int peopeleid){
accountDao.delete(peopeleid);//给到mapper 的是peopleid 接受的是id
}
}

浙公网安备 33010602011771号