案例: 根据id查询用户
public interface UserMapper {
List<User> getUserList();
//根据id查询用户
User findById(@Param("id") int id);
}
<select id="findById" resultType="com.ljs.mybatis.pojo.User">
select * from user where id=#{id}
select>
@Test
public void getUserById() {
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.findById(1);
System.out.println(user);
}
可以根据 密码 和 名字 查询用户
User findByNameAndPwd(@Param("name") String name, @Param("pwd") String paasword);
思路二:使用万能的Map
List<User> getListByNameAndPwd2(Map<String,Object> map);
<select id="getListByMap" parameterType="map" resultType="com.ljs.mybatis.pojo.User">
select * from user where name=#{name} and pwd=#{pwd}
select>
Map<String, Object> map = new HashMap<String, Object>();
map.put("name", "狂神");
map.put("pwd", "123456");
User user2 = mapper.findByNameAndPwd2(map);
总结: 如果参数过多,我们可以考虑直接使用Map实现,如果参数比较少,直接传递参数即可
我们一般使用insert标签进行插入操作,它的配置和select标签差不多!
需求:给数据库增加一个用户
int insert(User user);
<insert id="insert" parameterType="com.ljs.mybatis.pojo.User">
insert into user(id,name,pwd) values(#{id}, #{name}, #{pwd})
insert>
@Test
public void insert() {
User user = new User(4, "张无忌", "zhangwuji");
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
int insert = mapper.insert(user);
System.out.println(insert);
session.commit();//提交事务,重点!不写的话不会提交到数据库
session.close();
}
注意点:增、删、改操作需要提交事务!
需求:修改用户的信息
int updateUser(@Param("id")int id, @Param("pwd") String pwd);
int updateByUser(User user);
<update id="updateUser" parameterType="string">
update user set pwd = #{pwd} where id = #{id}
update>
<update id="updateUser" parameterType="com.ljs.mybatis.pojo.User">
update user set pwd = #{pwd}, name= #{name} where id = #{id}
update>
@Test
public void update() {
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.findById(1);
user.setPwd("112233");
int updateByUser = mapper.updateByUser(user);
int updateUser = mapper.updateUser(2, "222222");
System.out.println("updateUser==>>" + updateUser);
System.out.println("updateByUser==>>" + updateByUser);
session.commit();//提交事务,重点!不写的话不会提交到数据库
session.close();
}
根据id删除一个用户
int deleteById(String id);
<delete id="deleteById" parameterType="int">
delete from user where id = #{id}
delete>
@Test
public void delete() {
SqlSession session = MybatisUtils.getSessionFactory();
UserMapper mapper = session.getMapper(UserMapper.class);
int ok = mapper.deleteById(5);
}
List<User> getListByParams(@Param("name") String name);
<select id="getListByParams" resultType="com.ljs.mybatis.pojo.User">
select * from user where name like "%"#{name}"%"
select>
String name2 = "%"+name+"%";
List<User> getListByParams2(@Param("name") String name2);
<select id="getListByParams2" resultType="com.ljs.mybatis.pojo.User">
select * from user where name like #{name}
select>
排错:
标签必须比配一致
mybatis-config.xml的resource路径和mapper.xml中namespace路径
<mappers>
<mapper resource="com/ljs/mybatis/dao/xml/UserMapper.xml"/>
mappers>
<mapper namespace="com.ljs.mybatis.dao.UserMapper">
maven资源没有导出问题
向pom.xml中添加
<build>
<resources>
<resource>
<directory>src/main/javadirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
<resource>
<directory>src/main/resourcesdirectory>
<includes>
<include>**/*.propertiesinclude>
<include>**/*.xmlinclude>
includes>
<filtering>falsefiltering>
resource>
resources>
build>
小结:
掌握: