标签:pack map select标签 值类型 返回值 package ges list pre
我们在创建第一个项目自后,来进行数据库的操作;
Mybatis在JDBC的基础上,对sql语句的执行进行了简化。
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/java</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import java.util.List;
public interface UserMapper {
//查询所有的用户
public List<User> getUser();
//条件查询
public User getUserById(int id);
}
在UserMapper.xml文件中编写sql语句
<?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">
<mapper namespace="com.xiaoli.dao.UserMapper">
<!--查询全部用户-->
<select id="getUser" resultType="User">
select * from mybatis.user
</select>
<!--条件查询-->
<!--
select id="":方法名
parameterType="":传入的参数类型
resultType="":返回值类型
-->
<select id="getUserById" parameterType="int" resultType="com.xiaoli.pojo.User">
select * from mybatis.user where id = #{id}
</select>
</mapper>
测试代码与结果:
//获取所有的用户
@Test
public void testGetUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
List<User> user = mapper.getUser();
System.out.println("查询全部用户");
for (User user1 : user) {
System.out.println(user1);
}
sqlSession.close();
}
//条件查询
@Test
public void testGetUserById(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println("条件查询");
User userById = mapper.getUserById(3);
System.out.println(userById);
sqlSession.close();
}
测试结果:
全部查询
条件查询:
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import java.util.List;
public interface UserMapper {
//向数据库中插入数据
public int insertUser(User user);
}
在UserMapper.xml文件中编写sql语句
<?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">
<mapper namespace="com.xiaoli.dao.UserMapper">
<!--插入数据-->
<insert id="insertUser" parameterType="com.xiaoli.pojo.User">
insert into mybatis.user(id,username,password) values(#{id},#{username},#{password})
</insert>
</mapper>
测试代码与结果:
注意:在做增删改操作时,需要进行事务提交
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import com.xiaoli.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
//插入操作
//在做增删改操作时,需要进行事务提交
@Test
public void testInsertUser(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println("插入数据");
int i = mapper.insertUser(new User(8, "小虎", "27813"));
System.out.println(i);
//事务提交
sqlSession.commit();
sqlSession.close();
}
}
测试结果:
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import java.util.List;
public interface UserMapper {
//更新数据库中的数据
public int updateUserById(User user);
}
在UserMapper.xml文件中编写sql语句
<?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">
<mapper namespace="com.xiaoli.dao.UserMapper">
<!--更新数据-->
<!--
我们更新我们刚才插入的数据:8, "小虎", "27813" 更新为 8, “小花”,“34233”
-->
<update id="updateUserById" parameterType="com.xiaoli.pojo.User">
update mybatis.user set username = #{username},password = #{password} where id = #{id}
</update>
</mapper>
测试代码与结果:
注意:在做增删改操作时,需要进行事务提交
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import com.xiaoli.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
//更新操作
//在做增删改操作时,需要进行事务提交
@Test
public void testUpdateUserById(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println("更新数据");
//"小虎", "27813" 更新为 8, “小花”,“34233”
int i = mapper.updateUserById(new User(8, "小花", "34233"));
System.out.println(i);
//事务提交
sqlSession.commit();
sqlSession.close();
}
}
测试结果:
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import java.util.List;
public interface UserMapper {
//通过指定的Id删除用户
public int deleteUserById(int id);
}
在UserMapper.xml文件中编写sql语句
<?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">
<mapper namespace="com.xiaoli.dao.UserMapper">
<!--删除数据-->
<!--
例子:我们删除刚才插入的数据:id = 8
-->
<delete id="deleteUserById" parameterType="int">
delete from mybatis.user where id = #{id}
</delete>
</mapper>
测试代码与结果:
注意:在做增删改操作时,需要进行事务提交
package com.xiaoli.dao;
import com.xiaoli.pojo.User;
import com.xiaoli.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class UserDaoTest {
//删除操作
//在做增删改操作时,需要进行事务提交
@Test
public void testDeleteUserById(){
SqlSession sqlSession = MybatisUtil.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
System.out.println("删除数据");
//删除id = 8的用户
int i = mapper.deleteUserById(8);
System.out.println(i);
//事务提交
sqlSession.commit();
sqlSession.close();
}
}
测试结果:
标签:pack map select标签 值类型 返回值 package ges list pre
原文地址:https://www.cnblogs.com/code-xiaoli/p/14646026.html