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;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
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) ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
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) ;
}
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
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;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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) ;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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) ;
}