文章目录
三、Mybatis注解开发 3.1 快速入门
3.2 注解实现一对一映射
3.3 注解实现一对多映射
三、Mybatis注解开发
3.1 快速入门
3.1.1 常用注解说明
注解 功能 @Insert 新增 @Update 更新 @Delete 删除 @Select 查询 @Result 结果集封装 @Results 与@Result 一起使用,封装多个结果集 @One 一对一结果集封装 @Many 一对多结果集封装
3.1.2 注解实现开发
DROP TABLE IF EXISTS ` user` ;
CREATE TABLE ` user` (
` id` int ( 11 ) NOT NULL AUTO_INCREMENT ,
` username` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
` birthday` date NULL DEFAULT NULL ,
` sex` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
` address` varchar ( 255 ) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL ,
PRIMARY KEY ( ` id` ) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;
INSERT INTO ` user` VALUES ( 1 , '张三' , '1999-02-04' , '男' , '佛山' ) ;
INSERT INTO ` user` VALUES ( 2 , '李四' , '1998-01-15' , '女' , '湛江' ) ;
package com. dfbz. entity ;
import lombok. AllArgsConstructor ;
import lombok. Data ;
import lombok. NoArgsConstructor ;
import java. util. Date ;
import java. util. List ;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
}
package com. dfbz. dao ;
import com. dfbz. entity. User ;
import org. apache. ibatis. annotations. Delete ;
import org. apache. ibatis. annotations. Insert ;
import org. apache. ibatis. annotations. Select ;
import org. apache. ibatis. annotations. Update ;
import java. util. List ;
public interface UserDao {
@Select ( "select * from user where id=#{id}" )
User findById ( int id) ;
@Select ( "select * from user" )
List < User > findAll ( ) ;
@Insert ( "insert into user values(null,#{username},#{birthday},#{sex},#{address})" )
void save ( User user) ;
@Update ( "update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}" )
void update ( User user) ;
@Delete ( "delete from user where id=#{id}" )
void delete ( int id) ;
}
package com. dfbz. test ;
import com. dfbz. dao. UserDao ;
import com. dfbz. entity. 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. IOException ;
import java. util. Date ;
import java. util. List ;
public class Demo01 {
private SqlSessionFactoryBuilder builder;
private SqlSessionFactory factory;
private SqlSession session;
private UserDao userDao;
@Before
public void before ( ) throws IOException {
builder= new SqlSessionFactoryBuilder ( ) ;
factory= builder. build ( Resources . getResourceAsStream ( "SqlMapConfig.xml" ) ) ;
session= factory. openSession ( ) ;
userDao= session. getMapper ( UserDao . class ) ;
}
@After
public void after ( ) {
session. commit ( ) ;
session. close ( ) ;
}
@Test
public void save ( ) {
User user= new User ( ) ;
user. setUsername ( "小龙" ) ;
user. setBirthday ( new Date ( ) ) ;
user. setSex ( "男" ) ;
user. setAddress ( "广州" ) ;
userDao. save ( user) ;
}
@Test
public void delete ( ) {
userDao. delete ( 1 ) ;
}
@Test
public void update ( ) {
User user= new User ( ) ;
user. setId ( 2 ) ;
user. setUsername ( "张三" ) ;
user. setBirthday ( new Date ( ) ) ;
user. setSex ( "男" ) ;
user. setAddress ( "广州" ) ;
userDao. update ( user) ;
}
@Test
public void findById ( ) {
User user = userDao. findById ( 2 ) ;
System . out. println ( user) ;
}
@Test
public void findAll ( ) {
List < User > userList = userDao. findAll ( ) ;
for ( User user : userList) {
System . out. println ( user) ;
}
}
}
3.2 注解实现一对一映射
3.2.1 接口
package com. dfbz. entity ;
import lombok. Data ;
@Data
public class Emp {
private Integer id;
private String name;
private Integer age;
private String addr;
private Double salary;
private Dept dept;
}
package com. dfbz. entity ;
import lombok. Data ;
import java. util. List ;
@Data
public class Dept {
private Integer id;
private String name;
private String location;
private List < Emp > empList;
}
package com. dfbz. dao ;
import com. dfbz. entity. Dept ;
import org. apache. ibatis. annotations. Select ;
public interface DeptDao {
@Select ( "select * from dept where id=#{id}" )
Dept findById ( Integer id) ;
}
package com. dfbz. dao ;
import com. dfbz. entity. Dept ;
import com. dfbz. entity. Emp ;
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 ;
public interface EmpDao {
@Select ( "select * from emp where id=#{id}" )
@Results ( {
@Result ( id = true , property = "id" , column = "id" ) ,
@Result ( property = "name" , column = "name" ) ,
@Result ( property = "age" , column = "age" ) ,
@Result ( property = "addr" , column = "addr" ) ,
@Result ( property = "salary" , column = "salary" ) ,
@Result ( property = "dept" , column = "dept_id" , javaType = Dept . class ,
one = @One ( select = "com.dfbz.dao.DeptDao.findById" , fetchType = FetchType . LAZY )
) ,
} )
Emp findById ( Integer id) ;
}
3.2.2 测试类
@Test
public void test1 ( ) {
Emp emp = empDao. findById ( 1 ) ;
System . out. println ( emp) ;
}
3.3 注解实现一对多映射
3.2.1 接口
@Select ( "select * from dept where id=#{id}" )
@Results ( {
@Result ( id = true , property = "id" , column = "id" ) ,
@Result ( property = "name" , column = "name" ) ,
@Result ( property = "location" , column = "location" ) ,
@Result ( property = "empList" , column = "id" , javaType = List . class ,
many = @Many ( select = "com.dfbz.dao.EmpDao.findByDeptId" , fetchType = FetchType . LAZY )
)
} )
Dept findDeptAllById ( Integer id) ;
@Select ( "select * from emp where dept_id=#{deptId}" )
List < Emp > findByDeptId ( Integer deptId) ;
3.2.2 测试类
@Test
public void test2 ( ) {
Dept dept = deptDao. findDeptAllById ( 1 ) ;
System . out. println ( dept) ;
}