注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
注解CRUD
我们可以在工具类创建的时候实现自动提交事务!
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession(true);
}
编写接口,增加注解
public interface UserMapper {
@Select("select * from user")
List<User> getUsers();
//方法存在多个参数,每个参数必须加@Param注解
//参数从Param中取
@Select("select * from user where id =#{id}")
User getUserByID(@Param("id") int id);
@Insert("insert into user(id,name,pwd) values(#{id},#{name},#{pwd})")
int addUser(User user);
@Update("update user set name=#{name},pwd=#{pwd} where id =#{id}")
int updateUser(User user);
@Delete("delete from user where id =#{uid}")
int delete(@Param("uid") int id);
}
测试类
注意:必须将接口注册绑定到到核心配置文件中
<!--绑定接口-->
<mappers>
<mapper class="com.kuang.dao.UserMapper"/>
</mappers>
关于@Param()注解
- 基本类型的参数或者String类型,需要加上
- 引用类型不需要加
- 如果只有一个基本类型的话,可以忽略,建议都加上。
- 在SQL中引用的就是@Param()中设定的属性。
#{}防止sql注入,$无法防止sql注入
本文介绍了如何使用MyBatis的注解进行CRUD操作,包括UserMapper接口中的增删改查方法,并强调了@Param注解的使用规则。同时,提到了通过SqlSession工具类开启自动事务提交,以及#{}
2098

被折叠的 条评论
为什么被折叠?



