public interface UserMapper {
//@Select("select * from user")
List getUserList();
User getUserByID(int id);
//@Select("select * from user where name like concat('%',#{value},'%')")
List getUserLike(String value);
//分页
List getUserByLimit(Map map);
int addUser(User user);
int updateUser(User user);
int updateUser2(Map map);
int deleteUserByID(int id);
}
5.Mapper.xml
6.测试
@Test
public void getUser() {
//1.获取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//2.执行sql 方式一 getMapper
UserMapper userdao = sqlSession.getMapper(UserMapper.class);
List userList = userdao.getUserList();
User user1 = userdao.getUserByID(1);
// //2.执行sql 方式二
// List userList = sqlSession.selectList("com.lzq.dao.UserDao.getUserList");
// User user1 = sqlSession.selectOne("com.lzq.dao.UserDao.getUserByID",1);
for (User user : userList) {
System.out.println(user);
}
System.out.println(user1);
//3.关闭SqlSession
sqlSession.close();
}
select s.id sid,s.name sname,t.id tid,t.name tname
from student s,teacher t
where s.tid=t.id
select * from student
select * from teacher where id=#{tid}
一对多处理
一个老师对应多个学生
select * from teacher
select t.id tid,t.name tname,s.id sid,s.name sname
from teacher t,student s
where t.id=s.tid and t.id=#{tid}
select * from teacher where id=#{tid}
select * from student where tid=#{id}
其他标签的使用
and title like "%"#{title}"%"
and author like "%"#{author}"%"
select * from blog
select * from blog
title like "%"#{title}"%"author like "%"#{author}"%"views = #{views}
select * from blog
id=#{id}
update blog
title=#{title},author=#{author},views=#{views},
where id=#{id}