spring整合mybatis时,要使用mybatis的注解,需要spring-config.xml文件中添加下述配置:
<!--下述配置指明了我们的Mapper,即Dao,在相应的包里也就可以使用注解了,同时不用写Dao的实现类了 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.itszt.dao"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
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"> <!-- 保证命名空间,和接口的全类名一致 --> <mapper namespace="com.itszt.dao.UserDao"> <resultMap id="userMap" type="user"> <id property="uid" column="uid"></id> <result property="username111" column="username"></result> </resultMap> </mapper>
此时,在与UserDao.xml文件同一包下的UserDao接口可以直接写sql操作语句:
package com.itszt.dao; import com.itszt.domain.User; import com.itszt.domain.User2; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Repository; import java.util.List; /** * 我们给接口加@Repository注解,会根据我们的MapperScanner配置, *将来会自动为该接口构建匿名实现类,然后实例化这个实现类的对象拿去使用 * 而实现类的具体方法功能怎么实现,由两方面决定: * 1.UserDao.xml 2.interface UserDao中的注解 */ @Repository public interface UserDao { //演示常用查询 @Select("select * from user where uid=#{uid}") public User findUserByUID(int uid); @Select("select * from user where username=#{username} and userage=#{userage}") public User findUserByNameAndAge(@Param("username") String username, @Param("userage")int age); @Select("select count(*) from user") public Integer queryUserCount(); //演示insert ,并且获取自动递增的id赋值 @Insert("insert into user(username,userage,usersalary,regTime) values(#{username},#{userage},#{usersalary},#{regTime})") @SelectKey(keyProperty = "uid",keyColumn = "uid",resultType = Integer.class,before = false,statement = "select last_insert_id()") public void testaddUser1(User user); //演示insert ,并且获取自动设置UUID @Insert("insert into user2(uid,username,userage,usersalary,regTime) values(#{uid},#{username},#{userage},#{usersalary},#{regTime})") @SelectKey(keyProperty = "uid",keyColumn = "uid",resultType = String.class,before = true,statement = "select uuid()") public void testaddUser2(User2 user); //演示删除 @Delete("delete from user where uid=#{uid}") public void testDeleteUser(int uid); //演示更新某个字段 @Update("update user set username=#{username} where uid=#{uid}") public void testUpdateUser1(@Param("uid") int uid,@Param("username")String username); //演示ResultMap @Select("select * from user where uid=#{uid}") // @ResultMap("userMap") @Results({@Result(property = "username111",column = "username")}) public User testReusltRM(int uid); //演示list @Select("<script>" + "SELECT * FROM user WHERE uid IN " + "<foreach collection="list" open="(" close=")" separator="," item="uid"> " + " #{uid} " + "</foreach>"+ "</script>") public List<User> findUsersByIDs(List<Integer> uids); //演示list2,基于自定义SQL来做 @Select("SELECT * FROM user WHERE uid IN list:#{uids}") @Lang(SelectInLangDriver.class) public List<User> findUsersByIDs3(@Param("uids") List<Integer> uids); //演示list3,基于自定义SQL来做,能够支持多个集合 @Select("SELECT * FROM user WHERE uid IN list:#{uids} or userage IN list:#{ages}") @Lang(SelectInLangDriver2.class) public List<User> findUsersByIDs4(@Param("uids") List<Integer> uids,@Param("ages") List<Integer> ages); }