Java的增删改查

增删改一般没有返回值,查询全部一般是list<User> ,根据id查询是返回User对象,

insert into table表 (字段名1,字段名2... )values (值1,值2...);  

delete from table表 

update table表 set 字段名='要改的值' where 条件...

// 分页查询
PageInfo<User> findByPage(String companyId, int pageNum, int PageSize);

//查询所有部门
List<User> findAll(String companyId);

//保存
void save(User user);

//更新
void update(User user);

//删除
void delete(String id);

//根据id查询
User findById(String id);

扫描二维码关注公众号,回复: 10858139 查看本文章

用xml方式写sql语句的话:

<?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.dao.system.UserDao">
<resultMap id="BaseResultMap" type="cn.itcast.domain.system.User">
<id column="user_id" property="id"/>
<result column="dept_id" property="deptId"/>
<result column="email" property="email"/>
<result column="user_name" property="userName"/>
<result column="password" property="password"/>
<result column="state" property="state"/>
<result column="company_id" property="companyId"/>
<result column="company_name" property="companyName"/>
<result column="dept_name" property="deptName"/>
<result column="manager_id" property="managerId"/>
<result column="join_date" property="joinDate"/>
<result column="salary" property="salary"/>
<result column="birthday" property="birthday"/>
<result column="gender" property="gender"/>
<result column="station" property="station"/>
<result column="telephone" property="telephone"/>
<result column="degree" property="degree"/>
<result column="remark" property="remark"/>
<result column="order_no" property="orderNo"/>
</resultMap>

<!--根据企业id查询全部-->
<select id="findAll" resultMap="BaseResultMap">
select * from pe_user where company_id=#{companyId}
</select>

<!--根据id查询-->
<select id="findById" resultMap="BaseResultMap">
select * from pe_user where user_id = #{id}
</select>

<!--根据id删除-->
<delete id="delete">
delete from pe_user where user_id = #{id}
</delete>

<!--保存-->
<insert id="save">
insert into pe_user (user_id, dept_id, email, user_name, password, state, company_id,
company_name, dept_name, manager_id, join_date, salary, birthday,
gender, station, telephone, degree, remark, order_no)
values (#{id}, #{deptId}, #{email}, #{userName}, #{password}, #{state}, #{companyId},
#{companyName}, #{deptName}, #{managerId}, #{joinDate}, #{salary}, #{birthday},
#{gender}, #{station}, #{telephone}, #{degree}, #{remark}, #{orderNo})
</insert>

<!--更新-->
<update id="update">
update pe_user
set dept_id = #{deptId},
email = #{email},
user_name = #{userName},
password = #{password},
state = #{state},
company_id = #{companyId},
company_name = #{companyName},
dept_name = #{deptName},
manager_id = #{managerId},
join_date = #{joinDate},
salary = #{salary},
birthday = #{birthday},
gender = #{gender},
station = #{station},
telephone = #{telephone},
degree = #{degree},
remark = #{remark},
order_no = #{orderNo}
where user_id = #{id}
</update>
</mapper>

猜你喜欢

转载自www.cnblogs.com/binbinpeng/p/12714514.html