标签:
一:配置sqlMapConfig.xml(其他配置详见mybatis-xml配置随笔)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <properties resource="datasource.properties"></properties> <settings> <setting name="cacheEnabled" value="true"/> </settings> <!-- 将我们的pojo注册到配置文件 --> <typeAliases> <package name="com.wode.pojo" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <package name="com/wode/mapper" /> </mappers> </configuration>
二:普通增删改查
package com.wode.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.wode.pojo.User; public interface UserMapper { //使用注解的方式新增用户 @Insert("insert into users values(null,#{user.userName},#{user.userPwd},#{user.userType})") @Options(keyProperty="user.userId",useGeneratedKeys=true) public int addUser(@Param("user")User user); //注解的方式修改用户资料---多参数传递第二种方式 @Update("update users set user_name=#{name} where user_id=#{id}") public int updateUserNameById(@Param("name")String name,@Param("id")int id); //注解的方式删除用户 @Delete("delete from users where user_id=#{id}") public int delById(@Param("id") int id); @Select("select * from users") /** @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class) }) */ @ResultMap("userMap") public List<User> findAllUser(); }
二:常用查询
2.1:一对一
package com.wode.mapper; import java.util.List; import java.util.Map; import org.apache.ibatis.annotations.Delete; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.ResultMap; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import org.apache.ibatis.annotations.Update; import com.wode.pojo.User; public interface UserMapper { //使用注解的方式新增用户 @Insert("insert into users values(null,#{user.userName},#{user.userPwd},#{user.userType})") @Options(keyProperty="user.userId",useGeneratedKeys=true) public int addUser(@Param("user")User user); //注解的方式修改用户资料---多参数传递第二种方式 @Update("update users set user_name=#{name} where user_id=#{id}") public int updateUserNameById(@Param("name")String name,@Param("id")int id); //注解的方式删除用户 @Delete("delete from users where user_id=#{id}") public int delById(@Param("id") int id); @Select("select * from users") /** @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class) }) */ @ResultMap("userMap") public List<User> findAllUser(); @Select("select * from users where user_id=#{id}") @ResultMap("userMap") public User findUserById(@Param("id") int id); //1:1 @Select("select * from users where user_id=#{id}") @ResultMap("userMap") public User findUserAndInfo(@Param("id")int id); }
package com.wode.mapper; import java.util.List; import org.apache.ibatis.annotations.One; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; import org.apache.ibatis.annotations.Select; import com.wode.pojo.UserInfo; public interface UserInfoMapper { @Select("select * from userInfo") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class) }) public List<UserInfo> findUserInfo(); //user中外键查询数据 @Select("select * from userInfo where user_id=#{id}") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class) }) public UserInfo findByUser(@Param("id")int id); //1:1 @Select("select * from userinfo where info_id=#{info_id}") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class), @Result(column="user_id",property="user",one=@One(select="com.wode.mapper.UserMapper.findUserById")) }) public UserInfo findInfoAndUser(@Param("info_id") int id); }
2.2:一对多
public interface UserMapper { //使用注解的方式新增用户 @Insert("insert into users values(null,#{user.userName},#{user.userPwd},#{user.userType})") @Options(keyProperty="user.userId",useGeneratedKeys=true) public int addUser(@Param("user")User user); //注解的方式修改用户资料---多参数传递第二种方式 @Update("update users set user_name=#{name} where user_id=#{id}") public int updateUserNameById(@Param("name")String name,@Param("id")int id); //注解的方式删除用户 @Delete("delete from users where user_id=#{id}") public int delById(@Param("id") int id); @Select("select * from users") /** @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class) }) */ @ResultMap("userMap") public List<User> findAllUser(); @Select("select * from users where user_id=#{id}") @ResultMap("userMap") public User findUserById(@Param("id") int id); //一对多查询 @Select("select * from users where user_id=#{id}") @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class), @Result(property="info",column="user_id",many=@Many(select="com.wode.mapper.UserInfoMapper.findByUser")) }) public User findUserAndInfo(@Param("id")int id); }
public interface UserInfoMapper { @Select("select * from userInfo") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class) }) public List<UserInfo> findUserInfo(); //user中外键查询数据 @Select("select * from userInfo where user_id=#{id}") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class) }) public UserInfo findByUser(@Param("id")int id); @Select("select * from userInfo where info_id=#{infoId}") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class), @Result(column="user_id",property="user",one=@One(select="com.wode.mapper.UserMapper.findUserById")) }) public UserInfo findInfoAndUser(@Param("infoId")int infoId); }
2.3: 多对多
public interface UserMapper { //使用注解的方式新增用户 @Insert("insert into users values(null,#{user.userName},#{user.userPwd},#{user.userType})") @Options(keyProperty="user.userId",useGeneratedKeys=true) public int addUser(@Param("user")User user); //注解的方式修改用户资料---多参数传递第二种方式 @Update("update users set user_name=#{name} where user_id=#{id}") public int updateUserNameById(@Param("name")String name,@Param("id")int id); //注解的方式删除用户 @Delete("delete from users where user_id=#{id}") public int delById(@Param("id") int id); @Select("select * from users") /** @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class) }) */ @ResultMap("userMap") public List<User> findAllUser(); @Select("select * from users where user_id=#{id}") @ResultMap("userMap") public User findUserById(@Param("id") int id); //多对多查询 @Select("select * from users where user_id=#{id}") @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class), @Result(property="course",column="user_id",many=@Many(select="com.wode.mapper.CourseMapper.findCourseByUser")) }) public User findUserAndCourse(@Param("id")int id); @Select("select * from users where user_id in (select user_id from user_course where course_id=#{courseId})") @Results({ @Result(id=true,property="userId",column="user_id",javaType=Integer.class), @Result(property="userName",column="user_name",javaType=String.class), @Result(property="userPwd",column="user_pwd",javaType=String.class), @Result(property="userType",column="user_type",javaType=Integer.class) }) public User findUserByCourse(@Param("id")int courseId); }
public interface UserInfoMapper { @Select("select * from userInfo") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class) }) public List<UserInfo> findUserInfo(); //user中外键查询数据 @Select("select * from userInfo where user_id=#{id}") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class) }) public UserInfo findByUser(@Param("id")int id); @Select("select * from userInfo where info_id=#{infoId}") @Results({ @Result(id=true,column="info_id",property="infoId",javaType=Integer.class), @Result(column="nickName",javaType=String.class,property="nickName"), @Result(column="email",property="email",javaType=String.class), @Result(column="user_id",property="user",one=@One(select="com.wode.mapper.UserMapper.findUserById")) }) public UserInfo findInfoAndUser(@Param("infoId")int infoId); }
public interface CourseMapper { @Select("select * from course where course_id=#{id}") @Results({ @Result(id=true,column="course_id",property="courseId",javaType=Integer.class), @Result(column="code",property="code",javaType=String.class), @Result(column="name",property="name",javaType=String.class) }) public Course findCourseById(@Param("id")int id); @Select("select * from course where course_id in (select course_id from user_course where user_id=#{id})") @Results({ @Result(id=true,column="course_id",property="courseId",javaType=Integer.class), @Result(column="code",property="code",javaType=String.class), @Result(column="name",property="name",javaType=String.class) }) public Course findCourseByUser(@Param("uId")int id); @Select("select * from course where course_id=#{courseId}") @Results({ @Result(id=true,column="course_id",property="courseId",javaType=Integer.class), @Result(column="code",property="code",javaType=String.class), @Result(column="name",property="name",javaType=String.class), @Result(property="users",column="course_id",many=@Many(select="com.wode.mapper.UserMapper.findUserByCourse")) }) public Course findCourseAndUser(@Param("courseId")int courseId); }
在mapper包中的接口和xml文件链接,需:
SqlSession session = DbUtil.getSession(); UserInfoMapper mapper = session.getMapper(UserInfoMapper.class);
通过mapper.findUserInfoById(id)等方式实现增删改查
标签:
原文地址:http://www.cnblogs.com/xieshunjin/p/5797137.html