简单CRUD
package com.ttpfx.dao;
import com.ttpfx.domain.User;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserDao {
@Select("select id, username, birthday, sex, address from user")
List<User> getUserList();
@Select("select id, username, birthday, sex, address from user where username like #{keyword}")
List<User> getUserListByName(String keyword);
@Select("select id, username, birthday, sex, address from user where id = #{id}")
User getUserById(Integer id);
@Insert("insert into user (username, birthday, sex, address) values (#{username}, #{birthday}, #{sex}, #{address})")
int addUser(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);
}
关联查询之一对一
package com.ttpfx.dao;
import com.ttpfx.domain.Account;
import com.ttpfx.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
public interface AccountDao {
@Select("select id, uid, money from account")
@Results(id = "accountUserMap", value = {
@Result(id = true, column = "id", property = "id"),
@Result(column = "uid", property = "uid"),
@Result(column = "money", property = "money"),
@Result(column = "uid", property = "user", javaType = User.class, one = @One(select = "com.ttpfx.dao.UserDao.getUserById", fetchType = FetchType.EAGER))
})
List<Account> getAccountList();
@Select("select id, uid, money from account where id=#{id}")
@ResultMap({"accountUserMap"})
Account getAccountById(Integer id);
}
关联查询之一对多
package com.ttpfx.dao;
import com.ttpfx.domain.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import java.util.List;
public interface UserDao {
@Select("select id, username, birthday, sex, address from user")
@Results(id = "userAccountMap", value = {
@Result(id = true, column = "id", property = "id"),
@Result(column = "username", property = "username"),
@Result(column = "birthday", property = "birthday"),
@Result(column = "sex", property = "sex"),
@Result(column = "address", property = "address"),
@Result(column = "id", property = "accounts", many = @Many(select = "com.ttpfx.dao.AccountDao.getAccountListByUid", fetchType = FetchType.LAZY))
})
List<User> getUserList();
}