赞
踩
}
通过注解方式,就不需要再去编写 UserDao.xml 映射文件了。
编写 SqlMapConfig.xml 配置文件
SqlMapConfig.xml:
<?xml version="1.0" encoding="UTF-8"?>AnnotationCRUDTest:
package com.keafmd.test;
import com.keafmd.dao.IUserDao;
import com.keafmd.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
/**
Keafmd
@ClassName: AnnotationCRUDTest
@Description: 注解开发CRUD测试
@author: 牛哄哄的柯南
@date: 2021-02-16 21:05
*/
public class AnnotationCRUDTest {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private IUserDao userDao;
@Before
public void init() throws Exception{
in = Resources.getResourceAsStream(“SqlMapConfig.xml”);
factory = new SqlSessionFactoryBuilder().build(in);
session = factory.openSession();
userDao = session.getMapper(IUserDao.class);
}
@After
public void destory() throws Exception{
session.commit();
session.close();
in.close();
}
@Test
public void testSave(){
User user = new User();
user.setUsername(“mybatis annotation”);
user.setAddress(“北京”);
userDao.saveUser(user);
}
@Test
public void testUpdate(){
User user = new User();
user.setId(55);
user.setUsername(“mybatis annotation”);
user.setAddress(“北京”);
user.setSex(“男”);
user.setBirthday(new Date());
userDao.updateUser(user);
}
@Test
public void testDelete(){
userDao.deleteUser(54);
}
@Test
public void testFindOne(){
User user = userDao.findById(55);
System.out.println(user);
}
@Test
public void testFindByName(){
//List users = userDao.findByName(“%Keafmd%”);
List users = userDao.findByName(“Keafmd”);
for (User user : users) {
System.out.println(user);
}
}
@Test
public void testFindTotal(){
int total = userDao.findTotal();
System.out.println(total);
}
}
========================================================================
实现复杂关系映射之前我们可以在映射文件中通过配置
<resultMap>
来实现,在使用注解开发时我们需要借助@Results 注解,@Result 注解,@One 注解,@Many 注解。
**@Results 注解
代替的是标签
<resultMap>
该注解中可以使用单个@Result 注解,也可以使用@Result 集合
@Results({@Result(),@Result()})或@Results(@Result())**
**@Resutl 注解
代替了
<id>
标签和<result>
标签
@Result 中的属性介绍:**
| @Result 中的属性 | 介绍 |
| — | — |
| id | 是否是主键字段 |
| column | 数据库的列名 |
| property | 需要装配的属性名 |
| one | 需要使用的@One 注解(@Result(one=@One)())) |
| many | 需要使用的@Many 注解(@Result(many=@many)())) |
**@One 注解(一对一)
代替了
<assocation>
标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One 注解属性介绍:
select 指定用来多表查询的 sqlmapper
fetchType 会覆盖全局的配置参数 lazyLoadingEnabled。
使用格式:@Result(column=" “,property=”“,one=@One(select=”"))**
**@Many 注解(多对一)
代替了
<collection>
标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。
注意:聚集元素用来处理“一对多”的关系。需要指定映射的 Java 实体类的属性,属性的 javaType(一般为 ArrayList)但是注解中可以不定义。
使用格式:@Result(property=“”,column=“”,many=@Many(select=“”))**
需求:加载账户信息时并且加载该账户的用户信息,根据情况可实现立即加载。(注解方式实现)
User:
package com.keafmd.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
Keafmd
@ClassName: User
@Description: User实体类
@author: 牛哄哄的柯南
@date: 2021-02-16 20:28
*/
public class User implements Serializable {
private Integer userId;
private String userName;
private String userAddress;
private String userSex;
private Date userBirthday;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
@Override
public String toString() {
return “User{” +
“userId=” + userId +
“, userName='” + userName + ‘’’ +
“, userAddress='” + userAddress + ‘’’ +
“, userSex='” + userSex + ‘’’ +
“, userBirthday=” + userBirthday +
‘}’;
}
}
Account:
package com.keafmd.domain;
import java.io.Serializable;
/**
Keafmd
@ClassName: Account
@Description: 账户实体类
@author: 牛哄哄的柯南
@date: 2021-02-16 22:51
*/
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//多对一(mybatis中称之为一对一)的映射,一个账户只能属于一个用户 *
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return “Account{” +
“id=” + id +
“, uid=” + uid +
“, money=” + money +
‘}’;
}
}
IAccountDao:
package com.keafmd.dao;
import com.keafmd.domain.Account;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/**
Keafmd
@ClassName: IAccountDao
@Description:
@author: 牛哄哄的柯南
@date: 2021-02-16 22:54
*/
public interface IAccountDao {
/**
查询所有账户,并且获取每个账户下的用户信息,一对一
@return
*/
@Select(“select * from account”)
@Results(id=“accountMap”,value = {
@Result(id = true,column = “id”,property = “id”),
@Result(column = “uid”,property = “uid”),
@Result(column = “money”,property = “money”),
@Result(property = “user”,column = “uid”,one=@One(select=“com.keafmd.dao.IUserDao.findById”,fetchType= FetchType.EAGER))
})
List findAll();
}
package com.keafmd.dao;
import com.keafmd.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/**
Keafmd
@ClassName: IUserDao
@Description:
@author: 牛哄哄的柯南
@date: 2021-02-16 20:30
*/
/**
在mybatis中针对CRUD一共有四个注解
@Select @Insert @Update @Delete
*/
public interface IUserDao {
/**
查询所有用户
@return
*/
@Select(“select * from user”)
@Results(id=“userMap”,value={
@Result(id = true,column = “id”,property = “userId”),
@Result(column = “id”,property = “userId”),
@Result(column = “username”,property = “userName”),
@Result(column = “sex”,property = “userSex”),
@Result(column = “birthday”,property = “userBirthday”)
})
List findAll();
/**
根据id查询用户
@param userId
@return
*/
@Select(“select * from user where id=#{id}”)
//@ResultMap(value={“userMap”})
@ResultMap(“userMap”)
User findById(Integer userId);
/**
根据用户名称模糊查询
@param username
@return
*/
@Select(“select * from user where username like #{username}”) //占位符
@ResultMap(“userMap”)
List findByName(String username);
}
package com.keafmd.test;
import com.keafmd.dao.IAccountDao;
import com.keafmd.dao.IUserDao;
import com.keafmd.domain.Account;
import com.keafmd.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
/**
Keafmd
@ClassName: AnnotationCRUDTest
@Description: 注解开发CRUD测试
@author: 牛哄哄的柯南
@date: 2021-02-16 21:05
*/
public class AccountTest {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private IAccountDao accountDao;
@Before
public void init() throws Exception{
in = Resources.getResourceAsStream(“SqlMapConfig.xml”);
factory = new SqlSessionFactoryBuilder().build(in);
session = factory.openSession();
accountDao = session.getMapper(IAccountDao.class);
}
@After
public void destory() throws Exception{
session.commit();
session.close();
in.close();
}
@Test
public void testFindAll(){
List accounts = accountDao.findAll();
for (Account account : accounts) {
System.out.println(“-----每个账户信息-----”);
System.out.println(account);
System.out.println(account.getUser());
}
}
}
运行结果:
2021-02-17 03:04:39,939 163 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-17 03:04:40,190 414 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1176735295.
2021-02-17 03:04:40,190 414 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:04:40,195 419 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Preparing: select * from account
2021-02-17 03:04:40,226 450 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Parameters:
2021-02-17 03:04:40,267 491 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Preparing: select * from user where id=?
2021-02-17 03:04:40,268 492 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Parameters: 41(Integer)
2021-02-17 03:04:40,270 494 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <==== Total: 1
2021-02-17 03:04:40,271 495 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Preparing: select * from user where id=?
2021-02-17 03:04:40,271 495 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ====> Parameters: 45(Integer)
2021-02-17 03:04:40,272 496 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <==== Total: 1
2021-02-17 03:04:40,273 497 [ main] DEBUG keafmd.dao.IAccountDao.findAll - <== Total: 3
-----每个账户信息-----
Account{id=1, uid=41, money=1000.0}
User{userId=41, userName=‘update user clear cache’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 27 17:47:08 CST 2018}
-----每个账户信息-----
Account{id=2, uid=45, money=1000.0}
User{userId=45, userName=‘新一’, userAddress=‘null’, userSex=‘男’, userBirthday=Sun Mar 04 12:04:06 CST 2018}
-----每个账户信息-----
Account{id=3, uid=41, money=2000.0}
User{userId=41, userName=‘update user clear cache’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 27 17:47:08 CST 2018}
2021-02-17 03:04:40,274 498 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:04:40,274 498 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:04:40,274 498 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1176735295 to pool.
Process finished with exit code 0
**需求:查询用户信息时,也要查询他的账户列表。使用注解方式实现。
分析:一个用户具有多个账户信息,所以形成了用户(User)与账户(Account)之间的一对多关系。**
<Account>
User:
package com.keafmd.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
Keafmd
@ClassName: User
@Description: User实体类
@author: 牛哄哄的柯南
@date: 2021-02-16 20:28
*/
public class User implements Serializable {
private Integer userId;
private String userName;
private String userAddress;
private String userSex;
private Date userBirthday;
//一对多关系映射:一个用户对应多个账户
private List accounts;
public List getAccounts() {
return accounts;
}
public void setAccounts(List accounts) {
this.accounts = accounts;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserAddress() {
return userAddress;
}
public void setUserAddress(String userAddress) {
this.userAddress = userAddress;
}
public String getUserSex() {
return userSex;
}
public void setUserSex(String userSex) {
this.userSex = userSex;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
@Override
public String toString() {
return “User{” +
“userId=” + userId +
“, userName='” + userName + ‘’’ +
“, userAddress='” + userAddress + ‘’’ +
“, userSex='” + userSex + ‘’’ +
“, userBirthday=” + userBirthday +
‘}’;
}
}
package com.keafmd.dao;
import com.keafmd.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/**
Keafmd
@ClassName: IUserDao
@Description:
@author: 牛哄哄的柯南
@date: 2021-02-16 20:30
*/
/**
在mybatis中针对CRUD一共有四个注解
@Select @Insert @Update @Delete
*/
public interface IUserDao {
/**
查询所有用户
@return
*/
@Select(“select * from user”)
@Results(id=“userMap”,value={
@Result(id = true,column = “id”,property = “userId”),
@Result(column = “id”,property = “userId”),
@Result(column = “username”,property = “userName”),
@Result(column = “sex”,property = “userSex”),
@Result(column = “birthday”,property = “userBirthday”),
@Result(property = “accounts” ,column = “id”,
many = @Many(select = “com.keafmd.dao.IAccountDao.findAccountByUid”,
fetchType = FetchType.LAZY))
})
List findAll();
/**
根据id查询用户
@param userId
@return
*/
@Select(“select * from user where id=#{id}”)
//@ResultMap(value={“userMap”})
@ResultMap(“userMap”)
User findById(Integer userId);
/**
根据用户名称模糊查询
@param username
@return
*/
@Select(“select * from user where username like #{username}”) //占位符
@ResultMap(“userMap”)
List findByName(String username);
}
package com.keafmd.dao;
import com.keafmd.domain.Account;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
/**
Keafmd
@ClassName: IAccountDao
@Description:
@author: 牛哄哄的柯南
@date: 2021-02-16 22:54
*/
public interface IAccountDao {
/**
查询所有账户,并且获取每个账户下的用户信息,一对一 ,* 这里用不到这个findAll()
@return
*/
@Select(“select * from account”)
@Results(id=“accountMap”,value = {
@Result(id = true,column = “id”,property = “id”),
@Result(column = “uid”,property = “uid”),
@Result(column = “money”,property = “money”),
@Result(property = “user”,column = “uid”,one=@One(select=“com.keafmd.dao.IUserDao.findById”,fetchType= FetchType.EAGER))
})
List findAll();
/**
根据用户id查询账户信息
@param userId
@return
*/
@Select(“select * from account where uid = #{userId}”)
List findAccountByUid(Integer userId);
}
package com.keafmd.test;
import com.keafmd.dao.IUserDao;
import com.keafmd.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
/**
Keafmd
@ClassName: AnnotationCRUDTest
@Description: 注解开发CRUD测试
@author: 牛哄哄的柯南
@date: 2021-02-16 21:05
*/
public class UserTest {
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private IUserDao userDao;
@Before
public void init() throws Exception{
in = Resources.getResourceAsStream(“SqlMapConfig.xml”);
factory = new SqlSessionFactoryBuilder().build(in);
session = factory.openSession();
userDao = session.getMapper(IUserDao.class);
}
@After
public void destory() throws Exception{
session.commit();
session.close();
in.close();
}
@Test
public void testFindAll(){
List users = userDao.findAll();
for (User user : users) {
System.out.println(“-----每个用户的信息”);
System.out.println(user);
System.out.println(user.getAccounts());
}
}
}
运行testFindAll()的结果:
2021-02-17 03:14:19,655 378 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-17 03:14:20,124 847 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1176735295.
2021-02-17 03:14:20,124 847 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:14:20,135 858 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Preparing: select * from user
2021-02-17 03:14:20,183 906 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Parameters:
2021-02-17 03:14:20,471 1194 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - <== Total: 9
-----每个用户的信息
2021-02-17 03:14:20,473 1196 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,474 1197 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 41(Integer)
2021-02-17 03:14:20,475 1198 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 2
User{userId=41, userName=‘update user clear cache’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 27 17:47:08 CST 2018}
[Account{id=1, uid=41, money=1000.0}, Account{id=3, uid=41, money=2000.0}]
-----每个用户的信息
2021-02-17 03:14:20,476 1199 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,476 1199 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 42(Integer)
2021-02-17 03:14:20,477 1200 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=42, userName=‘update’, userAddress=‘null’, userSex=‘男’, userBirthday=Mon Feb 08 19:37:31 CST 2021}
[]
-----每个用户的信息
2021-02-17 03:14:20,479 1202 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,480 1203 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 43(Integer)
2021-02-17 03:14:20,481 1204 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=43, userName=‘小二王’, userAddress=‘null’, userSex=‘女’, userBirthday=Sun Mar 04 11:34:34 CST 2018}
[]
-----每个用户的信息
2021-02-17 03:14:20,481 1204 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,482 1205 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 45(Integer)
2021-02-17 03:14:20,483 1206 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 1
User{userId=45, userName=‘新一’, userAddress=‘null’, userSex=‘男’, userBirthday=Sun Mar 04 12:04:06 CST 2018}
[Account{id=2, uid=45, money=1000.0}]
-----每个用户的信息
2021-02-17 03:14:20,484 1207 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,484 1207 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 50(Integer)
2021-02-17 03:14:20,484 1207 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=50, userName=‘Keafmd’, userAddress=‘null’, userSex=‘男’, userBirthday=Mon Feb 08 15:44:01 CST 2021}
[]
-----每个用户的信息
2021-02-17 03:14:20,485 1208 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,485 1208 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 51(Integer)
2021-02-17 03:14:20,486 1209 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=51, userName=‘update DAO’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 09 11:31:38 CST 2021}
[]
-----每个用户的信息
2021-02-17 03:14:20,488 1211 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,489 1212 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 52(Integer)
2021-02-17 03:14:20,491 1214 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=52, userName=‘Keafmd DAO’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 09 11:29:41 CST 2021}
[]
-----每个用户的信息
2021-02-17 03:14:20,491 1214 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,492 1215 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 53(Integer)
2021-02-17 03:14:20,493 1216 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=53, userName=‘Keafmd laset insertid 1’, userAddress=‘null’, userSex=‘男’, userBirthday=Fri Feb 12 20:53:46 CST 2021}
[]
-----每个用户的信息
2021-02-17 03:14:20,493 1216 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:14:20,493 1216 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
2021-02-17 03:14:20,494 1217 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=55, userName=‘mybatis annotation’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 16 22:15:36 CST 2021}
[]
2021-02-17 03:14:20,495 1218 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:14:20,495 1218 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:14:20,495 1218 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1176735295 to pool.
Process finished with exit code 0
可以看出来延迟加载,在每次加载每个用户时都会查询一次。
修改测试代码:
@Test
public void testFindAll(){
List users = userDao.findAll();
/*for (User user : users) {
System.out.println(“-----每个用户的信息”);
System.out.println(user);
System.out.println(user.getAccounts());
}*/
}
运行结果:
2021-02-17 03:17:26,203 166 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-17 03:17:26,500 463 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1176735295.
2021-02-17 03:17:26,500 463 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:17:26,505 468 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Preparing: select * from user
2021-02-17 03:17:26,540 503 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Parameters:
2021-02-17 03:17:26,613 576 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - <== Total: 9
2021-02-17 03:17:26,614 577 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:17:26,614 577 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@46238e3f]
2021-02-17 03:17:26,614 577 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1176735295 to pool.
Process finished with exit code 0
这样通过对比就可以很明显的看出来延迟加载的效果。
=================================================================
运行代码:
@Test
public void testFindOne(){
User user = userDao.findById(55);
System.out.println(user);
User user2 = userDao.findById(55);
System.out.println(user2);
System.out.println(user==user2);
}
运行结果:
2021-02-17 03:26:22,736 164 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-17 03:26:22,970 398 [ main] DEBUG source.pooled.PooledDataSource - Created connection 85445963.
2021-02-17 03:26:22,971 399 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
2021-02-17 03:26:22,975 403 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id=?
2021-02-17 03:26:23,001 429 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 55(Integer)
2021-02-17 03:26:23,054 482 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
2021-02-17 03:26:23,055 483 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-17 03:26:23,055 483 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 55(Integer)
2021-02-17 03:26:23,056 484 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{userId=55, userName=‘mybatis annotation’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 16 22:15:36 CST 2021}
User{userId=55, userName=‘mybatis annotation’, userAddress=‘null’, userSex=‘男’, userBirthday=Tue Feb 16 22:15:36 CST 2021}
true
2021-02-17 03:26:23,056 484 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
2021-02-17 03:26:23,056 484 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
2021-02-17 03:26:23,057 485 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 85445963 to pool.
Process finished with exit code 0
===========================================================================
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)
前端面试题是我面试过程中遇到的面试题,每一次面试后我都会复盘总结。我做了一个整理,并且在技术博客找到了专业的解答,大家可以参考下:
on.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
2021-02-17 03:26:23,056 484 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@517cd4b]
2021-02-17 03:26:23,057 485 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 85445963 to pool.
Process finished with exit code 0
===========================================================================
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数前端工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Web前端开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-CgRqL08K-1711687830353)]
[外链图片转存中…(img-VvJOIPCE-1711687830353)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上前端开发知识点,真正体系化!
[外链图片转存中…(img-t0FtX1aH-1711687830354)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(资料价值较高,非无偿)
前端面试题是我面试过程中遇到的面试题,每一次面试后我都会复盘总结。我做了一个整理,并且在技术博客找到了专业的解答,大家可以参考下:
春招面试的后期,运气和实力都很重要,自己也是运气比较好,为了回馈粉丝朋友们(毕竟自己也玩了这么久哈哈哈),整理个人感悟和总结以上。最后祝愿大家能够收获理想offer!!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。