mybatis实现mySql上数据的增删改查,两种方式实现

刚刚学习了mybatis,实现了最基本的增删改查在这里记录一下,还是比较简单的:

  • 导入jar包:
    jar包
  • 编写主配置文件
<?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> 
    <settings>
        <!-- 打印SQL语句 -->
        <setting name="logImpl" value="STDOUT_LOGGING" />
    </settings>
    <environments default="development"> 
        <environment id="development"> 
            <!-- 事务和数据源 -->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
            	<property name="driver" value="com.mysql.jdbc.Driver"></property>
            	<property name="url" value="jdbc:mysql://localhost:3306/testdb?characterEncoding=utf-8"></property>
            	<property name="username" value="root"></property>
            	<property name="password" value=""></property>
            </dataSource>
        </environment>
    </environments> 
    <mappers>
    	<mapper resource="com/mapper/NewsMapper.xml"/>
    </mappers>
</configuration>

  • 编写实体类
package com.entity;

public class News {
	private int id;
	private String news_title;
	private String news_content;
	private String news_origin;
	private String news_time;
	
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getNews_title() {
		return news_title;
	}
	public void setNews_title(String news_title) {
		this.news_title = news_title;
	}
	public String getNews_content() {
		return news_content;
	}
	public void setNews_content(String news_content) {
		this.news_content = news_content;
	}
	public String getNews_origin() {
		return news_origin;
	}
	public void setNews_origin(String news_origin) {
		this.news_origin = news_origin;
	}
	public String getNews_time() {
		return news_time;
	}
	public void setNews_time(String news_time) {
		this.news_time = news_time;
	}
	@Override
	public String toString() {
		return "News [标题=" + news_title + ", 内容=" + news_content + ", 来源=" + news_origin
				+ ", 发布时间=" + news_time + "]";
	}
	
	
}

  • 编写映射器接口:
package com.mapper;

import java.util.List;

import com.entity.News;

public interface NewsMapper {
	//查询所有新闻
	public List<News> selectAll();
	//插入新闻
	public int insertNews(News news);
	//根据关键字查询新闻
	public List<News> selectPart(String news_content);
	//修改
	public int updateOrigin(News news);
	//删除
	public int deleteById(int id);
	//根据id查找对象
	public News selectById(int id);
}

  • 创建MyBatis的SQL映射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.mapper.NewsMapper">
	<select id="selectAll" resultType="com.entity.News">
	select * from news
	</select>
	<insert id="insertNews" parameterType="com.entity.News">
		insert into news values (#{id},#{news_title},#{news_content},#{news_origin},#{news_time})
	</insert>
	
	<select id="selectPart" resultType="com.entity.News">
		select * from news where news_content like "%"#{news_content}"%"
	</select>
	<select id="selectById" parameterType="int">
		select * from news where id= #{id}
	</select>
	<update id="updateOrigin" parameterType="com.entity.News">
		update news set news_origin=#{news_origin} where id=#{id} 
	</update>
  	<delete id="deleteById" parameterType="int">
  		delete from news where id=#{id}
  	</delete>
</mapper>
  • 编写工具类
package com.util;

import java.io.IOException;
import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtil {
	private static SqlSessionFactory sessionFactory;
	static {
		try {
			InputStream is=Resources.getResourceAsStream("mybatis.xml");
			sessionFactory=new SqlSessionFactoryBuilder().build(is);
			is.close();
		}catch(IOException e){
			e.printStackTrace();
		}
	}
	public static SqlSession getSession() {
		return sessionFactory.openSession();
	}

}

  • 编写代码进行测试
    第一种方式:
package com.ui;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.entity.News;
import com.mapper.NewsMapper;
import com.util.MyBatisUtil;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SqlSession session=MyBatisUtil.getSession();
		
		/*第一种方法*/
		
		//插入
		News news1=new News();
		news1.setId(2);
		news1.setNews_title("肖战王一博");
		news1.setNews_content("大家有各自的看法以及争论");
		news1.setNews_origin("微博");
		news1.setNews_time("2020-03-17");
		int n=session.insert("com.mapper.NewsMapper.insertNews",news1);
		System.out.println(n);
		if(n!=0) {
			System.out.println("插入成功");
		}else {
			System.out.println("插入失败");
		}
		
		//查询所有的新闻
		List<News> news=session.selectList("com.mapper.NewsMapper.selectAll");
		for(News newn:news) {
			System.out.println(newn);
		}
		//根据关键字查询新闻
		List<News> news2=session.selectList("com.mapper.NewsMapper.selectPart","肖战");	
		for(News newn:news2) {
			System.out.println(newn.toString());
		}
		if(news2.size()==0) {
			System.out.println("没有相关内容");
		}
		//更新
		
		News news3=new News();
		news3.setId(1);
		news3.setNews_origin("抖音");
		int num=session.update("com.mapper.NewsMapper.updateOrigin",news3);
		if(num!=0) {
			System.out.println("更新成功");
		}else {
			System.out.println("更新失败");
		}	
		session.commit();
		//删除
		int num1=session.delete("com.mapper.NewsMapper.deleteById",2);
		if(num1!=0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
	}

}

第二种方式:

package com.ui;

import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.entity.News;
import com.mapper.NewsMapper;
import com.util.MyBatisUtil;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		SqlSession session=MyBatisUtil.getSession();
		/*第二种方法:动态代理的机制*/
		NewsMapper newsMapper=session.getMapper(NewsMapper.class);
		//查询
		List<News> news=newsMapper.selectAll();
		for(News newn:news) {
			System.out.println(newn);
		}
		
		//插入
		News news1=new News();
		news1.setId(8);
		news1.setNews_title("肖战王一博");
		news1.setNews_content("王一博");
		news1.setNews_origin("微博");
		news1.setNews_time("2020-03-17");
		int n=newsMapper.insertNews(news1);
		if(n!=0) {
			System.out.println("插入成功");
		}else {
			System.out.println("插入失败");
		}
		session.commit();
		
		//模糊查询
		List<News> news2=newsMapper.selectPart("一");
		for(News newn:news2) {
			System.out.println(newn.toString());
		}
		if(news2.size()==0) {
			System.out.println("没有相关内容");
		}
		
		//更新
		
		News news3=new News();
		news3.setId(3);
		news3.setNews_origin("快手");
		int num=newsMapper.updateOrigin(news3);
		if(num!=0) {
			System.out.println("更新成功");
		}else {
			System.out.println("更新失败");
		}	
		session.commit();
		
		//删除
		int num1=newsMapper.deleteById(3);
		if(num1!=0) {
			System.out.println("删除成功");
		}else {
			System.out.println("删除失败");
		}
	}

}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值