Java Web学习笔记(一) Mapper接口实现MyBatis操作数据库

实训第一天笔记
Mapper.java

package com.example.mapper;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.example.bean.User;

/**
 * Created on 19/1/16
 * @author Chook_lxk
 * 
 * User的数据操作
 *
 */
@Repository
public interface UserMapper {

	int insert(User u);
	
	int update(User u);//User全属性更新
	
	int delete(Integer id);//User全属性匹配删除
	
	int deleteUser(User u);//User部分属性匹配删除
	
	int updateActive(User u);//User部分属性更新
	
	User selectId(Integer id);
	
	List<User> selectUser(User u);//查找部分属性相同的User
	
	List<User> selectAll(User u);//查找数据库中所有User
	
	int selectCount();//数据库中所有User的数量
}

mapper.xml



<mapper namespace="com.example.mapper.UserMapper">

	
	<resultMap
		id="base_map"
		type="User">
		<result
			column="id"
			property="id" />
		<result
			column="usercode"
			property="code" />
		<result
			column="username"
			property="name" />
		<result
			column="password"
			property="pass" />
	resultMap>

	
	<sql id="sql_column_list">
		id, usercode, username, password
	sql>

	<sql id="sql_where_fields">
		where 1=1
		<if test="id != null">and id = #{id}if>
		<if test="code != null and code != ''">and usercode = #{code}if>
		<if test="name != null and name != ''">and username = #{name}if>
		<if test="pass != null and pass != ''">and password = #{pass}if>
	sql>

	
	<insert
		id="insert"
		parameterType="Object">
		<selectKey
			resultType="int"
			order="AFTER"
			keyProperty="id">
			SELECT_LAST_INSERT_ID();
		selectKey>
		insert into edemo.user(usercode, username, password)
		values(#{code},#{name},#{pass})
	insert>

	
	<update
		id="update"
		parameterType="Object">
		update edemo.user set
		username=#{name},password=#{pass}
		where usercode=#{code}
	update>

	
	<update
		id="updateActive"
		parameterType="Object">
		update edemo.user set
		<trim suffixOverrides=",">
			<if test="username != null and username != ''">username = #{name},if>
			<if test="password != null and password != ''">password = #{pass},if>
		trim>
		where usercode = #{code}
	update>

	
	<delete
		id="delete"
		parameterType="Object">
		delete from edemo.user
		where id = #{id}
	delete>

	
	<delete
		id="deleteUser"
		parameterType="Object">
		delete from edemo.user
		<include refid="sql_where_fields" />
	delete>

	
	<select
		id="selectId"
		parameterType="Object"
		resultMap="base_map">
		select
		<include refid="sql_column_list" />
		from edemo.user where usercode = #{id}
	select>

	
	<select
		id="selectUser"
		parameterType="Object"
		resultMap="base_map">
		select
		<include refid="sql_column_list" />
		from edemo.user
		<include refid="sq_where_fields" />
	select>

	
	<select
		id="selectUser"
		parameterType="Object"
		resultMap="base_map">
		select
		<include refid="sql_column_list" />
		from edemo.user
	select>

	
	<select
		id="selectCount"
		resultType="int">
		select count(1) from edemo.user
	select>
mapper>
  • mapper.xml包含三部分 结果映射、Sql片段以及interface方法
  • 控制反转,实例化对象,交由Spring容器管理。 @Service(业务层) @Controller(控制层) @Repository (数据层)
  • 依赖注入,从Spring容器中获取对应对象 @Autowired
  • @RequestMapping() 通过路径调用Java类中的方法、接收参数

你可能感兴趣的:(Java学习,Java,Web学习笔记)