Mybatis连接JDBC环境搭建与使用(一)

1.,准备开发环境:Eclipse2019、Mysql 数据库、mybatis-3.5.2.jar 库、Mysql 驱动库。 接下来,创建 Web Project
在这里插入图片描述
2.在项目源代码目录 src 中新建xml文件,如 mybatis-config.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>
<!--搭建development 环境  -->
<environments default="development">
<!--引用development环境  -->
<environment id="development">
<!--使用JDBC事务  -->
<transactionManager type="JDBC"></transactionManager>
<!-- 获得数据库连接池 -->
<dataSource type="POOLED">
<!-- 创建驱动 -->
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/eee?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<!-- 这是你数据库的名字和密码 name="password"不可更改,value="123456"根据你数据库的密码进行更改-->
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--映射sql.xmlm 这个xml文件是Dao接口层目录下xxxmapper.xml的文件  -->
<mappers>
<mapper resource="com/wlh/dao/Dao.xml"/>
</mappers>
</configuration>

3.Dao写接口(这是一个编写好的增删改查的Dao接口 完整路径:com.wlh.dao.Dao)

package com.wlh.dao;

import java.util.List;
import com.wlh.entity.Stu;
public interface Dao {

	//查看所有
	public List<Stu> seeAll();
	//根据name模糊查询
	public List<Stu> seeByName(String name);
     //根据id查询一个
	public Stu seeById(int id);
	//根据id删除
	public int delById(int id);
	//修改
	public int upDate(Stu s);
	//add
	public int add(Stu s);
}

4.编写Dao.xml(Daomapper.xml完整路径为:com/wlh/dao/Dao.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">
<!-- 绑定Dao接口 com.wlh.dao.Dao 这是Dao的完整路径 -->
  <mapper namespace="com.wlh.dao.Dao">
  <!-- 绑定Dao接口里面的方法 id="seeAll"是Dao的方法名,
  resultType="stu"返回值类型(查看所有) -->
  <select id="seeAll" resultType="stu">
  SELECT * from t_model
  </select>
  <!--通过id查询 -->
  <select id="seeById" resultType="stu">
  SELECT * from t_model where id = #{id}
  </select>
    <!--通过name模糊查询 -->
  <select id="seeByName" resultType="stu">
  SELECT * from t_model where name LIKE  #{'%name%'}
  </select>
  <!-- 通过id删除-->
  <delete id="delById"  parameterType="int">
   DELETE FROM t_model where id = #{id}
  </delete>
 <!-- 通过id修改-->
  <update  id="upDate" parameterType="stu">
  UPDATE t_model SET name = #{name},url = #{url} where id = #{id}
  </update>
  <!-- 添加-->
  <insert id="add" parameterType="stu">
  INSERT into t_model (name,url) VALUES(#{name},#{url})
  </insert>
  <!--在编写完这个xml文件时,需要把这个文件映射到mytais.xml上-->
  </mapper>

5.因为有查询的功能,所以要把数据库里面的。记录封装到每个实体里面。所以新建一个实体类:

package com.wlh.entity;
public class Stu {

	private int id;
	private String name;
	private String url;
	public Stu() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Stu(int id, String name, String url) {
		super();
		this.id = id;
		this.name = name;
		this.url = url;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	@Override
	public String toString() {
		return "Stu [id=" + id + ", name=" + name + ", url=" + url + "]";
	}
	
}

6.测试

/*省略 package 和 import 代码*/ 
public class TestMybatis { 
 public static void main(String[] args) { 
 //声明一个sql文件
  String config = "mybatis-config.xml ";   
try {
          //读取资源
			InputStream in = Resources.getResourceAsStream(config);
         //创建sql资源
			SqlSessionFactoryBuilder builder = new  SqlSessionFactoryBuilder();
           //创建工厂
			SqlSessionFactory sqlSessionFactory = builder.build(in);
           //打开会话
			SqlSession sqlSession = sqlSessionFactory.openSession();
			//getMapper(Dao.class) 这是Dao的名字
			Dao mapper = sqlSession.getMapper(Dao.class);
			//获取Dao的方法
			List<Stu> list = mapper.seeAll();
		for (Stu stu : list) {
			System.out.println(stu);
		}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

}
} 

web路径
在这里插入图片描述
数据库连接jar包及mytaisjar包
链接:https://pan.baidu.com/s/1hLP7U9ddC5Q0BA3GNmNy9w
提取码:mz76
总纲
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值