mybatis 注解crud
1、接口
package com.wt.dao; import com.wt.pojo.User; import org.apache.ibatis.annotations.Select; import java.util.List; public interface UserMapper { @Select("select * from user") List<User> getUserList(); }
2、xml 配置文件
<mappers> <mapper class="com.wt.dao.UserMapper"/> </mappers>
3、测试使用
package com.wt.dao; import com.wt.pojo.User; import com.wt.utils.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserMapperTest { @Test public void getUserList(){ SqlSession sqlSession = MyBatisUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } }
局限性
注解不是适用于复杂的sql语句(官网原话),没有resultMap 数据库字段名和javaBean的字段名不同
案例
注解的crud
接口类
package com.wt.dao; import com.wt.pojo.User; import org.apache.ibatis.annotations.*; import java.util.List; public interface UserMapper { @Select("select * from user") List<User> getUserList(); /* @Param 注解 1. 基本数据类型和 String类型 添加 改注解 2. 其它引用数据类型不添加此注解 3. 注解的名称,是查询语句的变量名 4. xml映射文件 和 注解都可以使用 5. 单个变量,加不加 Param 没有区别,但推荐添加 */ // 查 @Select("select * from mybatis.user where id = #{uid}") User getUserById(@Param("uid") int id); // 增 @Insert("insert into user(name, pwd) values(#{name}, #{password})") int insertUser(@Param("name") String name, @Param("password") String pwd); // 改 @Update("update user set name= #{username} where id = #{uid}") int updateUser(@Param("username") String name, @Param("uid") int id); // 删 @Delete("delete from user where id = #{uid}") int deleteUserByiD(@Param("uid") int id); }
2、xml配置文件 mapper同上 用 class
3、测试
package com.wt.dao; import com.wt.pojo.User; import com.wt.utils.MyBatisUtil; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.List; public class UserMapperTest { @Test public void getUserList(){ SqlSession sqlSession = MyBatisUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); List<User> userList = userMapper.getUserList(); for (User user : userList) { System.out.println(user); } sqlSession.close(); } @Test public void getUserByiD(){ SqlSession sqlSession = MyBatisUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User userById = userMapper.getUserById(1); System.out.println(userById); sqlSession.close(); } @Test public void insertUser(){ SqlSession sqlSession = MyBatisUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); int insertId = userMapper.insertUser("to", "12345"); sqlSession.commit(); System.out.println(insertId); sqlSession.close(); } @Test public void updateUser(){ SqlSession sqlSession = MyBatisUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.updateUser("xx", 6); sqlSession.commit(); sqlSession.close(); } @Test public void deleteUser(){ SqlSession sqlSession = MyBatisUtil.getSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); userMapper.deleteUserByiD(5); sqlSession.commit(); sqlSession.close(); } }