-
VO、PO、DTO、BO、POJO、DAO、Entity的区别
- VO:value object:值对象
通常用于业务层之间的数据传递,由new创建,由GC回收;和PO一样也是仅仅包含数据而已,但应是抽象出的业务对象,可以和表对应,也可以不是。 - PO:persistant object:持久层对象
是ORM(Objevt Relational Mapping)框架中Entity,PO属性和数据库中表的字段形成一一对应关系;VO和PO,都是属性加上属性的get和set方法;表面看没什么不同,但代表的含义是完全不同的。 - DTO:data transfer object:数据传输对象
是一种设计模式之间传输数据的软件应用系统,数据传输目标往往是数据访问对象从数据库中检索数据;数据传输对象与数据交互对象或数据访问对象之间的差异是一个以不具任何行为除了存储和检索的数据(访问和存取器);简而言之,就是接口之间传递的数据封装;DTO由此产生,一是能提高数据传输的速度(减少了传输字段),二能隐藏后端表结构。 - BO:business object:业务对象
BO把业务逻辑封转为一个对象,通过调用DAO方法,结合PO或VO进行业务操作;PO组合,如投保人是一个PO,被保险人是一个PO,险种信息是一个PO等等,他们组合起来是第一张保单的BO。 - POJO:plian ordinary java object:简单无规则java对象
纯的传统意义的 Java 对象,最基本的 JavaBean 只有属性加上属性的 get 和 set 方法;可以额转化为PO、DTO、VO;比如POJO在传输过程中就是DTO。 - DAO:data access object:数据访问对象
是sun的一个标准 J2EE 设计模式,这个模式中有个接口就是DAO,负责持久层的操作;主要用来封装对数据的访问,注意,是对数据的访问,不是对数据库的访问;DAO对数据的访问。 - Entity:实体,和PO的功能类似,和数据表一一对应,一个实体一张表。
- VO:value object:值对象
-
第二种实现mybatis的方式为:接口+xml方式
- 这种方式有特别的要求
1. namespace必须是接口的全路径
2. 每个节点的id必须是接口中的方法名
3. 该接口中的方法不允许重载,否则namespace+id将不唯一
4. 注意该接口中的增删改的方法的返回值,最好使用int
- 这种方式有特别的要求
-
User.java
@Data @NoArgsConstructor @AllArgsConstructor public class User { private int uid; private String username; private String password; private int age; private String addr; }
-
IUserDao.java
public interface IUserDao { /** * 查询所有的User对象 * @return User对象的列表 */ List
getAllUsers(); /** * 新增一个User对象 * @param u 要新增的用户对象 * @return 受影响的行数 */ int saveUser(User u); } -
UserMapper.xml
insert into user values(null, #{username}, #{password}, #{age}, #{addr}) update user set username = #{username}, password =#{password}, age=#{age}, addr = #{addr} where uid = #{uid} -
SessionUtils.java
public class SessionUtils { private static SqlSession mSession = null; private static SqlSessionFactory mFactory = null; static { try { mFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis.xml")); } catch (IOException e) { e.printStackTrace(); } } /** * 获取SqlSession对象 * @return */ public static SqlSession getSession(){ mSession = mFactory.openSession(true); return mSession; } /** * 关闭SqlSession对象 * @param session 要关闭的SqlSession对象 */ public static void closeSession(SqlSession session){ if(session != null){ session.close(); session = null; } } }
-
Mybatis.xml
-
TestUser.java
public class TestUser { @Test public void testUser(){ User u = new User(); u.setUid(1); u.setUsername("BBB"); u.setPassword("111111"); u.setAddr("ABCD"); u.setAge(18); System.out.println(u); } }
-
TestUserDao.java
public class TestUserDao { private SqlSession session = null; @Before public void setUp(){ session = SessionUtils.getSession(); } @After public void tearDown(){ SessionUtils.closeSession(session); } @Test public void testGetAllUsers(){ IUserDao mapper = session.getMapper(IUserDao.class); List
users = mapper.getAllUsers(); for (User u : users) { System.out.println(u); } } @Test public void testSaveUser(){ IUserDao userDao = session.getMapper(IUserDao.class); User u = new User(17, "AAA", "111111", 20, "ABCD"); int result = userDao.saveUser(u); System.out.println(result); } } -
TestUserDao2.java
public class TestUserDao2 { private SqlSession mSession = null; @Before public void setUp(){ mSession = SessionUtils.getSession(); } @After public void tearDown(){ SessionUtils.closeSession(mSession); } @Test public void testGetAllUsers(){ IUserDao2 dao2 = mSession.getMapper(IUserDao2.class); List
list = dao2.getAllUsers(); for (User u : list) { System.out.println(u); } } @Test public void testGetUserByUid(){ IUserDao2 dao2 = mSession.getMapper(IUserDao2.class); User u = dao2.getUserByUid(15); System.out.println(u); } @Test public void testSaveUser(){ IUserDao2 dao2 = mSession.getMapper(IUserDao2.class); User u = new User(15, "ABC", "111111", 20, "南京"); int i = dao2.saveUser(u); System.out.println(i); } @Test public void testLogin(){ IUserDao2 dao2 = mSession.getMapper(IUserDao2.class); User u = dao2.login("ABC", "111111"); System.out.println(u); } } -
测试结果
Opening JDBC Connection Created connection 2088445230. ==> Preparing: insert into user values(null, ?, ?, ?, ?) ==> Parameters: ABC(String), 111111(String), 20(Integer), 南京(String) <== Updates: 1 1 Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7c7b252e] Returned connection 2088445230 to pool. Opening JDBC Connection Checked out connection 2088445230 from pool. ==> Preparing: select * from user where username=? and password=? ==> Parameters: ABC(String), 111111(String) <== Columns: uid, username, password, age, addr <== Row: 15, ABC, 111111, 20, 南京 <== Total: 1 User(uid=15, username=ABC, password=111111, age=20, addr=南京) Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7c7b252e] Returned connection 2088445230 to pool. Opening JDBC Connection Checked out connection 2088445230 from pool. ==> Preparing: select * from user ==> Parameters: <== Columns: uid, username, password, age, addr <== Row: 1, aaa, 111111, 10, aaaa <== Row: 2, bbb, 222222, 11, aaaa <== Row: 3, ccc, 333333, 12, aaaa <== Row: 4, ddd, 444444, 13, aaaa <== Row: 5, eee, 555555, 14, aaaa <== Row: 6, fff, 666666, 15, aaaa <== Row: 7, ggg, 777777, 16, aaaa <== Row: 8, hhh, 888888, 17, aaaa <== Row: 9, iii, 999999, 18, aaaa <== Row: 10, jjj, 000000, 19, aaaa <== Row: 11, kkk, 000000, 20, aaaa <== Row: 12, lkt, 888888, 20, suzhou <== Row: 15, ABC, 111111, 20, 南京 <== Total: 13 User(uid=1, username=aaa, password=111111, age=10, addr=aaaa) User(uid=2, username=bbb, password=222222, age=11, addr=aaaa) User(uid=3, username=ccc, password=333333, age=12, addr=aaaa) User(uid=4, username=ddd, password=444444, age=13, addr=aaaa) User(uid=5, username=eee, password=555555, age=14, addr=aaaa) User(uid=6, username=fff, password=666666, age=15, addr=aaaa) User(uid=7, username=ggg, password=777777, age=16, addr=aaaa) User(uid=8, username=hhh, password=888888, age=17, addr=aaaa) User(uid=9, username=iii, password=999999, age=18, addr=aaaa) User(uid=10, username=jjj, password=000000, age=19, addr=aaaa) User(uid=11, username=kkk, password=000000, age=20, addr=aaaa) User(uid=12, username=lkt, password=888888, age=20, addr=suzhou) User(uid=15, username=ABC, password=111111, age=20, addr=南京) Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@7c7b252e] Returned connection 2088445230 to pool. Opening JDBC Connection Checked out connection 2088445230 from pool. ==> Preparing: select * from user where uid = ? ==> Parameters: 15(Integer) <== Columns: uid, username, password, age, addr <== Row: 15, ABC, 111111, 20, 南京 <== Total: 1 User(uid=15, username=ABC, password=111111, age=20, addr=南京) Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@5656be13] Returned connection 1448525331 to pool.