当前位置: 代码迷 >> 综合 >> JAVA之mybatis基本操作(CURD)
  详细解决方案

JAVA之mybatis基本操作(CURD)

热度:33   发布时间:2023-11-19 11:13:48.0

今天我们来操作下mybatis的基本操作,增删改查。

我们有个数据库,id,username,address,birthday,sex

有个User类,对应数据库元素,

我们看总配置文件sqlMapConfig.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 和spring整合后 environments配置将废除,所以,我们不做多的介绍--><environments default="development"><environment id="development"><!-- 使用jdbc事务管理 --><transactionManager type="JDBC" /><!-- 数据库连接池 --><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url"value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8" /><property name="username" value="root" /><property name="password" value="root" /></dataSource></environment></environments><!-- 操作user数据库,在这里注册 --><mappers><mapper resource="sqlmap/User.xml"/></mappers>
</configuration>

然后就是我们的user.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:命名空间,用于隔离sql, -->
<mapper namespace="test">
<!-- 单个查询 -->
<select id="findAll" parameterType="Integer"  resultType="com.fei.my.pojo.User" >
select * from user where id = #{v}
</select><!-- 模糊查询 -->
<select id="find" parameterType="String" resultType="com.fei.my.pojo.User">
select * from user where username like '%${value}%'
</select><!-- 插入语句 -->
<insert id="insert" parameterType="com.fei.my.pojo.User">
<!-- 设置返回主键 -->
<selectKey keyProperty="id" resultType="Integer" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
<!-- 插入语句 -->
insert into user(username,birthday,sex,address)values(#{username},#{birthday},#{sex},#{address})
</insert><!-- 更新操作 -->
<update id="update" parameterType="com.fei.my.pojo.User">
update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
</update><!-- 删除操作 -->
<delete id="delete" parameterType="Integer">
delete from user where id = #{id}
</delete></mapper>

在每个的操作上与注解,然后我们用一个测试类来试试:

	@Testpublic void test() throws IOException {String resource = "sqlMapConfig.xml";InputStream in = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);SqlSession sqlSession = sqlSessionFactory.openSession();User u = new User();
//	单个查询User user = sqlSession.selectOne("test.findAll", 1);

查询单个id为1的数据

我们把整个代码贴出:

package com.fei.my.test;import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;import com.fei.my.pojo.User;public class TestOne {@Testpublic void test() throws IOException {String resource = "sqlMapConfig.xml";InputStream in = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);SqlSession sqlSession = sqlSessionFactory.openSession();User u = new User();
//	单个查询
//		User user = sqlSession.selectOne("test.findAll", 1);//		模糊查询,查看结果list遍历
//		List<User> user = sqlSession.selectList("test.find","三");//		插入操作		
//		u.setUsername("赵钱孙");
//		u.setAddress("河北邯郸");
//		u.setBirthday(new Date());
//		u.setSex("男");
//		sqlSession.insert("test.insert",u);		
//		sqlSession.commit();
//		查询返回的主键
//		System.out.println(u.getId());//		更新操作
//		u.setId(1);
//		u.setAddress("sss");
//		u.setBirthday(new Date());
//		u.setSex("nv");
//		u.setUsername("xxx");
//		int i =sqlSession.update("test.update",u);
//		sqlSession.commit();
//		System.out.println(i);//		删除操作
//		sqlSession.delete("test.delete",22);
//		sqlSession.commit();
//		System.out.println("结束");}}

每个操作有注解,大家可以看下

  相关解决方案