MyBatis-2 CURD操作

写在前面

有了前面的基础,下一步我们就可以进行数据库的基本增删改查操作了,为了让结果更加直观,这里我们使用log4j进行日志输出。另外,如果你还没有创建工程,请移步MyBatis-1 MyBatis基本配置
log4j下载:

链接:https://pan.baidu.com/s/1bwMb5IDW9CHc9gqCDjDU6w
提取码:ree1

将log4j-1.2.17.jar导入lib下,接着在src目录下创建log4j.properties文件,输入下面的代码。

# Global logging configuration
log4j.rootLogger=ERROR, stdout
# MyBatis logging configuration...
# 这里请修改为自己工程的包路径。
log4j.logger.com.test=DEBUG
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

数据库结构:
数据库结构
下面进入本期主题。

select

mapper(部分):

 	<select id="selectAllCustomer" resultType="com.test.po.Customer">
 		select * from demo1
 	</select>
 	<!-- 模糊查询使用字符串拼接'%' -->
 	<select id="selectCustomerByName" resultType="com.test.po.Customer">
 		select * from demo1 where username like '%'+ #{username}+'%'
 	</select>

测试类:

	@Test
	public void findCustomers() throws IOException{
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//创建sqlsession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		//sqlSession执行文件映射,返回结果
		List<Customer> list = sqlSession.selectList("com.test.mapper.CustomerMapper.selectAllCustomer");
		System.out.println(list.toString());
		sqlSession.close();
	}

运行结果:
select操作结果

insert

mapper(部分):

<!-- 由于id是自增长字段,所以不需要输入 -->
<insert id="insertCustomerInfo" parameterType="com.test.po.Customer">
 		insert into demo1 values(#{username},#{jobs},#{phone})
 	</insert>

测试类:

	@Test
	public void insertCustomerInfo()throws Exception {
		String resource = "mybatis-config.xml";
		InputStream inputStream = Resources.getResourceAsStream(resource);
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
		//创建sqlsession
		SqlSession sqlSession = sqlSessionFactory.openSession();
		Customer customer = new Customer();
		customer.setJobs("job");
		customer.setUsername("una");
		customer.setPhone("123123123");
		int i = sqlSession.insert("com.test.mapper.CustomerMapper.insertCustomerInfo", customer);
		//如果影响了1行以上,则打印success,反之error
		if(i>0){
			System.out.println("success");
		}
		else{
			System.out.println("error");
		}
		//提交事务
		sqlSession.commit();
		sqlSession.close();
	}

运行结果:
插入成功

update

mapper(部分):

<!-- 推荐参数都统一使用对象,而不是传递单个参数 -->
<update id="updateCustomerInfo" parameterType="com.test.po.Customer">
 		update demo1 set username=#{username},job=#{jobs},phone=#{phone} where id = #{id}
 	</update>

测试类:

		@Test
		public void updateCustomerInfo()throws Exception {
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			//创建sqlsession
			SqlSession sqlSession = sqlSessionFactory.openSession();
			Customer customer = new Customer();
			customer.setId(2);
			customer.setJobs("job");
			customer.setUsername("una");
			customer.setPhone("11111");
			int i = sqlSession.update("com.test.mapper.CustomerMapper.updateCustomerInfo", customer);
			if(i>0){
				System.out.println("success");
			}
			else{
				System.out.println("error");
			}
			//提交事务
			sqlSession.commit();
			sqlSession.close();
		}

运行结果:
更新数据成功

delete

mapper(部分):

	<!-- 推荐参数都统一使用对象,而不是传递单个参数 -->
 	<delete id="deleteCustomerInfo" parameterType="com.test.po.Customer">
 		delete from demo1 where id = #{id}
 	</delete>

测试类:

		@Test
		public void deleteCustomerInfo()throws Exception {
			String resource = "mybatis-config.xml";
			InputStream inputStream = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			//创建sqlsession
			SqlSession sqlSession = sqlSessionFactory.openSession();
			Customer customer = new Customer();
			customer.setId(1);
			int i = sqlSession.update("com.test.mapper.CustomerMapper.deleteCustomerInfo", customer);
			if(i>0){
				System.out.println("success");
			}
			else{
				System.out.println("error");
			}
			//提交事务
			sqlSession.commit();
			sqlSession.close();
		}

运行结果:
在这里插入图片描述

总结&常见错误

1.执行增删改操作时,要提交事务。
2.参数尽量都使用对象来传递。
3.待补充。。。

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值