Mybatis简单入门配置

1. 导入Mybatis包

在github上下载jar包

https://github.com/mybatis/mybatis-3
打开上边的网页,然后拉倒最底下,点击链接进行下载。下载完解压后将mybatis.jar和lib下的所有jar包都进行导入。

Maven配置

https://github.com/mybatis/mybatis-3

里面有关于Mybatis的Maven配置

<dependencies>
	<dependency>
	  <groupId>org.mybatis</groupId>
	  <artifactId>mybatis</artifactId>
	  <version>3.5.9</version>
	</dependency>
	<dependency>
	  <groupId>mysql</groupId>
	  <artifactId>mysql-connector-java</artifactId>
	  <version>8.0.27</version>
	</dependency>
	<dependency>
      <groupId>ognl</groupId>
      <artifactId>ognl</artifactId>
      <version>3.3.0</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.javassist</groupId>
      <artifactId>javassist</artifactId>
      <version>3.28.0-GA</version>
      <scope>compile</scope>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.32</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-api</artifactId>
      <version>2.17.0</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
      <optional>true</optional>
    </dependency>
    <dependency>
      <groupId>cglib</groupId>
      <artifactId>cglib</artifactId>
      <version>3.3.0</version>
      <optional>true</optional>
    </dependency>
</dependencies>

2. 配置Mybatis

SqlMapConfig.xml文件配置

SqlMapConfig.xml文件是Mbatis的主要配置文件,eclipse将文件放在src/main/java目录下,idea将文件放在resources目录下。

<?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>

	<!-- 配置数据库链接 -->
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC" />
			<!-- 配置数据库连接信息 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.cj.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/test" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	
	<!-- 加载POJO类的数据库操作文件 -->
	<mappers>
		<mapper resource="mapper/UserMapper.xml"/>
	</mappers>
	
</configuration>

UserMapper.xml文件配置

UserMapper.xml里面编写了对User表的相应操作

<?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="userMapper">

	<!-- id类似于方法名,后边用来指定调用的 -->
	<!-- resultType指定返回的结果类型,mybatis会自动根据变量名进行装配 -->
	<select id="queryAll" resultType="model.User">
		select * from user
	</select>
	
	<!-- parameterType指定传入sql语句的类型,可以为基本数据类型,也可以是封装类型例如Integer -->
	<!-- 注意参数的取值要用花括号{} -->
	<select id="queryById" parameterType="int" resultType="model.User">
		select * from user where id = #{id}
	</select>
	
	<insert id="insert" parameterType="model.User">
		insert into user(id,name) values(#{id},#{name})
	</insert>
	
	<!-- mybatis会根据参数名自动从对象中获取参数值 -->
	<update id="update" parameterType="model.User">
		update user set name = #{name} where id = #{id}
	</update>
	
	<delete id="delete" parameterType="java.lang.Integer">
		delete from user where id = #{id}
	</delete>
	
</mapper>

3. 测试

3.1 增

	public void insert() {
		SqlSession session = null;
		try {
			String resource = "SqlMapConfig.xml";
			// 获取核心配置文件信息
			InputStream inputStream = Resources.getResourceAsStream(resource);
			// 获取session工厂对象
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			inputStream.close();
			// 创建会话,不会自动提交事务
			session = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		User user = new User();
		user.setId(1);
		user.setName("hello word");
		//通过namespace.id来进行查找方法
		session.insert("userMapper.insert", user);
		session.commit();
		if (session != null) {
			session.close();
		}
	}

3.2 删

	public void delete() {
		SqlSession session = null;
		try {
			String resource = "SqlMapConfig.xml";
			// 获取核心配置文件信息
			InputStream inputStream = Resources.getResourceAsStream(resource);
			// 获取session工厂对象
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			inputStream.close();
			// 创建会话,不会自动提交事务
			session = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		session.delete("userMapper.delete", 1);
		session.commit();
		if (session != null) {
			session.close();
		}
	}

3.3 改

	public void update() {
		SqlSession session = null;
		try {
			String resource = "SqlMapConfig.xml";
			// 获取核心配置文件信息
			InputStream inputStream = Resources.getResourceAsStream(resource);
			// 获取session工厂对象
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			inputStream.close();
			// 创建会话,不会自动提交事务
			session = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		User user = new User(10);
		user.setId(1);
		user.setName("hello mybatis");
		session.update("userMapper.update", user);
		session.commit();
		if (session != null) {
			session.close();
		}
	}

3.4 查

public void query() {
		SqlSession session = null;
		try {
			String resource = "SqlMapConfig.xml";
			// 获取核心配置文件信息
			InputStream inputStream = Resources.getResourceAsStream(resource);
			// 获取session工厂对象
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			inputStream.close();
			// 创建会话,不会自动提交事务
			session = sqlSessionFactory.openSession();
		} catch (IOException e) {
			e.printStackTrace();
		}
		List<User> list = session.selectList("userMapper.queryAll");
		for(User u:list) {
			System.out.println("id:"+u.getId()+"name:"+u.getName());
		}
		if (session != null) {
			session.close();
		}
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值