【MyBatis】学习笔记10:添加功能获取自增的主键
【MyBatis】学习笔记03:配置文件进一步解读(非常重要)
【MyBatis】学习笔记06:各种查询所返回数据的数据类型
JDBC的方式
@Test
public void testJDBC() throws Exception{
Class.forName("");
Connection connection = DriverManager.getConnection("","","");
PreparedStatement ps = connection.prepareStatement("");
ps.executeUpdate();
ResultSet resultSet = ps.getGeneratedKeys();
}
MyBatis的方式
/**
* 新增用户
* @param user 用户对象
*/
void insertUser(User user);
<!-- useGeneratedKeys:设置当前标签中的SQL使用自增主键-->
<!-- keyProperty:将自增的主键的值传输到映射文件中参数的某个属性-->
<!-- void insertUser(User user);-->
<insert id="insertUser" useGeneratedKeys="true" keyProperty="userId">
insert into t_users set
userName=#{userName},
userCount=#{userCount},
userPassword=#{userPassword}
</insert>
public void testGetUserByLike(String username){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
List<User> list = mapper.getUserByLike(username);
System.out.println(list);
}
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
SQLMapper mapper = sqlSession.getMapper(SQLMapper.class);
User user = new User("照刘狗","zliho","123456");
mapper.insertUser(user);
testGetUserByLike("狗");
}
版 权 声 明