maybatis--注解查询
maybatis使用注解查询的时候,便不能使用配置查询,否则会报错
单表操作代码示例:
1.主配置文件不变
<?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="db.properties"/> <!--配置驼峰命名法,且settings属性要在文件environments上面--> <settings> <setting name="mapUnderscoreToCamelCase" value="true" /> </settings> <!-- 运行环境配置(整合后归Spring) --> <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> <!-- 指定映射文件 如果用注解配置:需要偶配置class的全限定类名--> <mappers> <!--<mapper resource="com/dao/UserMapper.xml" />--> <!--<mapper class="com.dao.UserMapper"></mapper>--> <package name="com.dao"></package> </mappers> </configuration>
2.编写实体类
public class User { private Integer id; private String uname; private String pwd; }
3.DAO层实体类 注解编写
public interface UserMapper { @Select("select * from user") List<User> findAll(); @Insert("insert into user(uname,pwd) values(#{uname},#{pwd})") int insertUser(User user); @Select("select * from user where id=#{id}") User find(Integer id); @Update("update user set uname=#{uname},pwd=#{pwd} where id=#{id}") int updateUser(User user); @Delete("delete from user where id=#{id}") int deleteUser(Integer id); }
4.测试--增删改查
package com; import com.bean.User; import com.dao.UserMapper; import com.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class AnnotationTest01 { //查询所有 @Test public void m1(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> all = mapper.findAll(); for (User user : all) { System.out.println(user); } session.commit(); session.close(); } //添加User @Test public void m2(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = new User(); user.setUname("欧布"); user.setPwd("asc"); mapper.insertUser(user); session.commit(); session.close(); } //查询单个User @Test public void m3(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.find(1); System.out.println(user); session.commit(); session.close(); } //修改User @Test public void m4(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); User user = mapper.find(2); user.setPwd("123"); int i = mapper.updateUser(user); session.commit(); session.close(); } //删除User @Test public void m5(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); mapper.deleteUser(2); session.commit(); session.close(); } }
一对多查询示例,多的一方
实体类
public class Account { private Integer id; private String name; private Integer money; //一对多 多的一方 private User user; }
DAO层
public interface AccountMapper { @Select("select * from account") List<Account> findAll(); @Select("select * from account") @Results(id = "accountMap",value = { @Result(id=true,column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "money",property = "money"), @Result(column = "uid",property = "user", one = @One(select = "com.dao.UserMapper.find",fetchType = FetchType.EAGER) )//select = "com.dao.UserMapper.find",调用一的一方必须是查询单个find,而不是findAll。 }) List<Account> findAllAU(); }
测试
//查询所有 @Test public void m1(){ SqlSession session = MybatisUtils.getSession(); AccountMapper mapper = session.getMapper(AccountMapper.class); //List<Account> all = mapper.findAll(); List<Account> all = mapper.findAllAU(); for (Account account : all) { System.out.println(account+":"+account.getUser()); } session.commit(); session.close(); }
一对多查询示例,一的一方
实体类
public class User { private Integer id; private String uname; private String pwd; private List<Account> accounts; }
DAO层
public interface UserMapper { @Select("select * from user") List<User> findAll(); @Insert("insert into user(uname,pwd) values(#{uname},#{pwd})") int insertUser(User user); @Select("select * from user where id=#{id}") User find(Integer id); @Update("update user set uname=#{uname},pwd=#{pwd} where id=#{id}") int updateUser(User user); @Delete("delete from user where id=#{id}") int deleteUser(Integer id); //对多查询 @Select("select * from user") @Results(id = "userMap", value = { @Result(id = true, column = "id", property = "id"), @Result(column = "uname", property = "uname"), @Result(column = "pwd", property = "pwd"), @Result(property = "accounts", column = "id", many = @Many( select = "com.dao.AccountMapper.findByuId", fetchType = FetchType.LAZY ))//findByuId,方法的uid取外键,FetchType.LAZY懒加载 }) List<User> findAllUA(); }
测试
//查询所有 一的一方 @Test public void m2(){ SqlSession session = MybatisUtils.getSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<User> allUA = mapper.findAllUA(); for (User user : allUA) { System.out.println(user.getUname()); for (Account account : user.getAccounts()) { System.out.println(account); } } session.commit(); session.close(); }