电商项目开发6

电商项目开发6

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

用户管理

UserDao.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">
<!-- 命名空間,xml文件和dao接口对应起來 -->
<mapper namespace="com.zq.dao.UserDao">
	<resultMap type="com.zq.model.User" id="userMap">
		<result property="id" column="id"/><!-- column是数据库字段的名字 property是java里面属性名 -->
		<result property="username" column="user_name"/><!-- column是数据库字段的名字 property是java里面属性名 -->
		<result property="pwd" column="user_pwd" /><!-- mybatis plugin插件快捷键 Alt+/ -->
		<result property="realname" column="realname" />
		<result property="grade" column="grade" />
		<result property="total" column="total" />
		<result property="account" column="account" />
	</resultMap>

	<select id="findById" parameterType="integer" resultType="user">
		select * from user where id = #{id}
	</select>



	<sql id="sqlWhere">
		<where><!-- 这种写法会自动去掉第一个and -->
			<if test="username!=null and username!=''">
				and username = #{username}
			</if>
			<if test="pwd!=null and pwd!=''">
				and pwd = #{pwd}
			</if>
			<!-- id与字符串判断区别 -->
			<if test="id !=null">
				and id = #{id}
			</if>
			<if test="realname!=null and realname!=''">
				and realname like concat('%',#{realname},'%')<!-- -->
			</if>
		</where>
	</sql>
	<!-- 查询列表 -->
	<select id="list" parameterType="user" resultMap="userMap">
		select * from user
		<include refid="sqlWhere" />
	</select>

	<!-- id不需要,自增 -->
	<insert id="create" parameterType="user">
		insert into
		user(username,pwd,realname)
		values(#{username},#{pwd},#{realname})
	</insert>

	<update id="update" parameterType="user">
		update user
		<set>
			<if test="username!=null and username!=''">
				username = #{username},
			</if>
			<if test="pwd!=null and pwd!=''">
				pwd = #{pwd},
			</if>
			<if test="realname!=null and realname!=''">
				realname = #{realname},
			</if>
		</set>
		where id = #{id}
		<!-- set username = #{username},pwd=#{pwd} where id = #{id} -->
	</update>

	<delete id="delete" parameterType="integer">
		delete from user where id =	#{id}
	</delete>
	
	<!-- 批量操作 -->
	<update id="updateBatch" parameterType="List"><!-- List<Integer> -->
		update user set pwd = '123' where id in 
		<foreach item="item" index="index" collection="list" open="("
			separator="," close=")">
			#{item}
		</foreach>
	</update>

	<select id="findByTotal" parameterType="list" resultType="user">
		select * from user 
		<where>
			<foreach item="item" index="index" collection="list" open=" ( " 
				separator=" ) or ( " close=" ) ">
					grade = #{item.grade} - 1 and total &gt;= #{item.total}<!-- 大于和小于号要用转义符 -->
					<!-- 另一种写法<![CDATA[grade = #{item.grade} - 1 and total >= #{item.total}]]>  -->
			</foreach>
		</where>
	</select>
	
	<update id="updateGrade" parameterType="List"><!-- List<User>类型 -->
		update user set grade = grade + 1 where id in 
		<foreach item="item" index="index" collection="list" open="("
			separator="," close=")">
			#{item.id}
		</foreach>
	</update>
</mapper>

UserDao.java

package com.zq.dao;

import java.util.List;
import java.util.Map;

import com.zq.model.User;

public interface UserDao {	
	public List<User> list(User user);
	public void create(User user);
	public void update(User user);
	public void delete(Integer id);
	public void updateBatch(List<Integer> list);
	public User findById(Integer id);
	public List<User> findByTotal(List<Map<String, Object>> gradeList);
	public void updateGrade(List<User> users);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值