User.mapper.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.dxj.mapper.DeptMapper"> <resultMap id="DeptMap" type="Dept"> <id column="de_id" jdbcType="INTEGER" property="id" /> <result column="de_name" jdbcType="VARCHAR" property="name" /> <result column="de_loc" jdbcType="VARCHAR" property="loc" /> <result column="de_time" property="date" /> <result column="de_by" jdbcType="VARCHAR" property="person" /> </resultMap> <!-- 增加 --> <insert id="save"> INSERT INTO dept_tab(de_name,de_loc,de_time,de_by) VALUES(#{name},#{loc},#{date},#{person}) </insert> <!-- 全查询 --> <select id="findAll" resultMap="DeptMap"> SELECT * FROM dept_tab </select> <!-- 修改--> <update id="update"> UPDATE dept_tab <set> <if test="upd.name!=null"> de_name=#{upd.name} </if> <if test="upd.loc!=null"> de_loc=#{upd.loc} </if> <if test="upd.date!=null"> de_time=#{upd.date} </if> <if test="upd.person!=null"> de_by=#{upd.person} </if> </set> WHERE de_id=#{upd.id} </update> <!-- 删除--> <delete id="del"> DELETE FROM dept_tab WHERE de_id=#{id} </delete> </mapper>
#{}是预编译处理,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值,可以有效的防止SQL注入,提高系统安全性;
${}是字符串替换,把${}替换成变量的值;