在Mybatis.xml中配置
<!--注册接口-->
<mappers>
<mapper class="com.Google.Dao.userMapper"/>
</mappers>
增
//增加
@Insert("insert into user (id,name,pwd) values (#{id},#{name},#{pwd})")
int insert(@Param("id") int id,@Param("name") String name,@Param("pwd") String Password);
```java
public void insert (){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
userMapper mapper = sqlSession.getMapper(userMapper.class);
int i = mapper.insert(5, "xixi","23234234");
if(i>0){
sqlSession.commit();
}
sqlSession.close();
}
删
//删除
@Delete("delete from user where id = #{id}")
int Delete (int id);
@Test
public void Delete (){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
userMapper mapper = sqlSession.getMapper(userMapper.class);
int i = mapper.Delete(1);
if(i>0){
sqlSession.commit();
}
sqlSession.close();
}
改
//修改
@Update("update user set name=#{name} where id=#{id}")
int update (@Param("name") String name,@Param("id") int id);
@Test
public void update (){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
userMapper mapper = sqlSession.getMapper(userMapper.class);
int i = mapper.update("haha", 1);
if(i>0){
sqlSession.commit();
}
sqlSession.close();
}
查
//查询
@Select("select * from user where id=#{id}")
User getUserByID1(@Param("id") int id);
public void getUserByID(){
SqlSession sqlSession = sqlSessionFactory.getsqlSession();
userMapper mapper = sqlSession.getMapper(userMapper.class);
User user = mapper.getUserByID1(2);
System.out.println(user);
sqlSession.close();
}