0.注解的基本理解
1.基本CRUD
1.1编写实体类User
public class User {
private Integer id;
private String username;
private String sex;
private Date birthday;
private String address;
//省略getter,setter,tostring方法
}
1.2编写持久层接口Userdao
public interface Userdao {
//查询所有
@Select("select * from user")
List<User> findAll();
//根据用户查询一个用户
@Select("select * from user where id =#{uid}")
User findById(Integer id);
//插入操作
@Insert("insert into user (username,birthday,sex,address)values(#{username},#{birthday},#{sex},#{address}) ")
int insertUser(User user);
//更新
@Update("update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}")
int updateUser(User user);
//删除
@Delete("delete from user where id=#{id}")
int deleteUser(Integer id);
//查询使用聚合函数
@Select("select count(*) from user")
int findTotal();
//模糊查询
@Select("select * from user where username like #{username}")
List<User> findByName(String name);
}
1.3测试
public class Mytest1 {
private InputStream in;
private SqlSession sqlSession;
private Userdao userdao;
@Before
public void init() throws Exception {
//1.读取配置文件
in= Resources.getResourceAsStream("SqlMapConfig.xml");
//2.获取SqlSessionFactory对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
//3.获取Sqlsession对象
sqlSession = factory.openSession();
//4.使用Sqlsession创建接口的代理对象
userdao = sqlSession.getMapper(Userdao.class);
//Userdao userdao = sqlSession.getMapper(Userdao.class);
}
//@After
public void des() throws IOException {
//6.释放资源
sqlSession.commit();
sqlSession.close();
//in.close();
}
//插入
@Test
public void testInsert() throws Exception {
User user=new User();
user.setUsername("annotation");
user.setSex("男");
user.setAddress("合肥");
user.setBirthday(new Date());
int res=userdao.insertUser(user);
System.out.println(res);
//System.out.println(user.getId());
des();
}
//更新
@Test
public void testUpdate() throws Exception {
User user = userdao.findById(54);
System.out.println(user);
des();
}
//删除
@Test
public void testDelete() throws Exception {
userdao.deleteUser(55);
des();
}
//聚合函数
@Test
public void testJuhe() throws Exception {
int total = userdao.findTotal();
System.out.println(total);
des();
}
//模糊查询
@Test
public void testMohu() throws Exception {
List<User> users = userdao.findByName("%王%");
for (User user : users) {
System.out.println(user);
}
des();
}
//查询一个
@Test
public void testFindById() throws Exception {
User user = userdao.findById(54);
System.out.println(user);
des();
}
}
2.多对一(Mybatis中为一对一)
2.1同1中编写User实体类
2.2编写Account实体类
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//多对一映射,从表应该添加一个主表的对象引用
private User user;
//省略getter,setter,tostring方法
}
2.3添加Account持久层接口并使用注解配置
public interface Accountdao {
@Select("select *from account")
@Results(id="accountmap",value = {
@Result(id=true,column = "id",property = "id"),
@Result(column = "uid",property = "uid"),
@Result(column = "monry",property = "money"),
@Result(column = "uid",
property = "user",
one=@One(select="com.dao.Userdao.findById",
fetchType = FetchType.LAZY)
),
})
List<Account> findAll();
}
通过字段uid,使用findbyid查询用户。
2.4添加User持久层接口并使用注解配置
public interface Userdao {
//查询所有
@Select("select * from user")
@Results(id="usermap",
value={
@Result(id=true,column="id",property = "id"),
@Result(column="username",property = "username"),
@Result(column="sex",property = "sex"),
@Result(column="birthday",property = "birthday"),
@Result(column="address",property = "address"),
})
List<User> findAll();
//根据用户查询一个用户
@Select("select * from user where id =#{uid}")
@ResultMap("usermap")
User findById(Integer id);
}
2.5测试
//多对一
@Test
public void testmoretoone(){
List<Account> accounts = accountdao.findAll();
for (Account account : accounts) {
System.out.println(account);
System.out.println(account.getUser());
}
2.6结果
3.一对多
3.1在User实体类中加入List
private List<Account> accounts;
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
3.2编写User持久层接口并使用注解配置
public interface Userdao {
//查询所有
@Select("select * from user")
@Results(id="usermap",
value={
@Result(id=true,column="id",property = "id"),
@Result(column="username",property = "username"),
@Result(column="sex",property = "sex"),
@Result(column="birthday",property = "birthday"),
@Result(column="address",property = "address"),
@Result(column = "id",property = "accounts",
many=@Many(
select="com.dao.Accountdao.findByUid",
fetchType = FetchType.LAZY
))
})
List<User> findAll();
3.3编写Account持久层接口并使用注解配置
@Select("select * from account where uid=#{uid}")
List<Account> findByUid(Integer id);
3.4测试
@Test
public void testonetomore(){
List<User> users=userdao.findAll();
for (User user : users) {
System.out.println(user);
System.out.println(user.getAccounts());
}
}
3.5结果