Mybatis可以自动生成增删改查的sql.xml文件吗
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis可以自动生成增删改查的sql.xml文件吗相关的知识,希望对你有一定的参考价值。
参考技术A 你好,mybatis是可以自动生成的,生成后的sql.xml文件中包含了基本的增删改查的操作,其他的操作可以自定义。具体的生成步骤你可以百度一下有很多种 参考技术B 不可以的,这个需要你手动去配置的
Mybatis-增删改查之事务的提交
依据Mybatis中的Mybatis配置,进行的增删改查的操作
增
UserMapperTest.calss
@Test
public void addUser() throws IOException {
SqlSession sqlsession = MybatisUtil.getSQLSession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
int row = mapper.addUser(new UserPojo(5, "hahahaha", "xxxxxx"));
System.out.println(row);
sqlsession.commit();
sqlsession.close();
}
删
@Test
public void deleteUser() throws IOException {
SqlSession sqlsession = MybatisUtil.getSQLSession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
int row = mapper.deleteUser(5);
System.out.println(row);
sqlsession.commit();
sqlsession.close();
}
改
@Test
public void updateUser() throws IOException {
SqlSession sqlsession = MybatisUtil.getSQLSession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
int row = mapper.updateUser(new UserPojo(5, "xiugai", "xiugaile"));
System.out.println(row);
sqlsession.commit();
sqlsession.close();
}
查
@Test
public void getUserList() throws IOException {
SqlSession sqlsession = MybatisUtil.getSQLSession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
List<UserPojo> listUser = mapper.getListUser();
sqlsession.close();
for (UserPojo user : listUser) {
System.out.println(user);
}
}
UserMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace相当于(UserMapperImpl)实现dao层接口-->
<mapper namespace="com.test.mapper.UserMapper">
<!--查询所有用户-->
<select id="getListUser" resultType="com.test.pojo.UserPojo">
select * from user
</select>
<!--添加用户-->
<insert id="addUser" parameterType="com.test.pojo.UserPojo">
insert into user values(#{id},#{name},#{password});
</insert>
<!--修改用户-->
<update id="updateUser" parameterType="com.test.pojo.UserPojo">
update user set name=#{name},password=#{password} where id=#{id}
</update>
<!--删除用户-->
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id}
</delete>
</mapper>
可能存在的错误
- 标签不要匹配错误
- resource绑定mapper,需要使用路径(可以使用包名,但是UserMapper.class和UserMapper.xml需要在同一个包下)
- 配置文件需要符合规范
- NullPointerException,没有注册到资源(SQLSession资源为局部,没有获取到SQLSession资源)
- 输出的xml文件中存在中文乱码问题
- maven资源没有导出问题(静态资源文件过滤问题)
万能Map传递参数
UserMapperTest.class
//使用Map传递参数
@Test
public void addUserMap() throws IOException {
SqlSession sqlsession = MybatisUtil.getSQLSession();
UserMapper mapper = sqlsession.getMapper(UserMapper.class);
Map<String,Object> map = new HashMap<String, Object>();
map.put("id",7);
map.put("password","oooo");
map.put("name","6666");
int row = mapper.addUserMap(map);
System.out.println(row);
sqlsession.commit();
sqlsession.close();
}
UserMapper.class(接口)
package com.test.mapper;
import com.test.pojo.UserPojo;
import java.util.List;
import java.util.Map;
public interface UserMapper {
/*增加User使用Map传递参数*/
public int addUserMap(Map<String,Object> map);
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!--namespace相当于(UserMapperImpl)实现dao层接口-->
<mapper namespace="com.test.mapper.UserMapper">
<!--添加用户-->
<insert id="addUser" parameterType="com.test.pojo.UserPojo">
insert into user values(#{id},#{name},#{password});
</insert>
<!--添加用户使用Map传递参数-->
<insert id="addUserMap" parameterType="map">
insert into user values(#{id},#{name},#{password});
</insert>
</mapper>
以上是关于Mybatis可以自动生成增删改查的sql.xml文件吗的主要内容,如果未能解决你的问题,请参考以下文章