01.Mybatis初级使用-初体验

首次使用Mybatis

0.新建maven工程,引入依赖

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>
  	
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>3.4.2</version>
</dependency>

1.在resource下新建jdbc.properties、roleMapper.xml、mysql-config.xml文件

jdbc.proerties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
user=root
password=luzhen

com/lz/mybatis/mapper/roleMapper.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">
  
<mapper namespace="com.lz.mybatis.model.RoleMapper">
	<select id="getRole" parameterType="java.lang.Integer" resultType="Role">
		select * from role where id = #{id}
	</select>
</mapper>


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>
	<properties resource="jdbc.properties"/>
	<typeAliases>
		<package name="com.lz.mybatis.model"/>
	</typeAliases>
	<environments default="development">
		<environment id="development">
			<transactionManager type="JDBC"/>
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="user" value="${user}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<package name="com.lz.mybatis.mapper"/>
	</mappers>
</configuration> 


2.在src下新建Role.java、RoleMapper.java、MybatisUtil.java、MainApp.java

Role.java

package com.lz.mybatis.model;

public class Role {

	private Integer id;
	
	private String roleName;
	
	private String note;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	@Override
	public String toString() {
		return "Role [id=" + id + ", roleName=" + roleName + ", note=" + note + "]";
	}
	
}


RoleMapper.java

package com.lz.mybatis.mapper;

import com.lz.mybatis.model.Role;

public interface RoleMapper {
	Role getRole(Integer id);
}

MybatisUtil.java

package com.lz.mybatis.util;

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

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

public class MybatisUtil {

	private static SqlSessionFactory sqlSessionFactory = null;

	public static SqlSessionFactory getSqlSessionFactory() {
		if (sqlSessionFactory == null) {
			String resource = "mybatis-config.xml";
			try {
				InputStream inputStream = Resources.getResourceAsStream(resource);
				sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		return sqlSessionFactory;
	}

}

MainApp.java

package com.lz.mybatis.app;

import org.apache.ibatis.session.SqlSession;

import com.lz.mybatis.mapper.RoleMapper;
import com.lz.mybatis.model.Role;
import com.lz.mybatis.util.MybatisUtil;

public class MainApp {

	public static void main(String[] args) {
		SqlSession session = null;
		try {
			session = MybatisUtil.getSqlSessionFactory().openSession();
			RoleMapper roleMapper = session.getMapper(RoleMapper.class);
			Role role = roleMapper.getRole(10001);
			System.out.println(role);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (session != null) {
				session.close();
			}
		}
	}

}

工程目录



运行程序,得到结果


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
[INFO] Scanning for projects... [ERROR] [ERROR] Some problems were encountered while processing the POMs: [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 73, column 21 [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 85, column 21 [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 82, column 21 @ [ERROR] The build could not read 3 projects -> [Help 1] [ERROR] [ERROR] The project org.pw:user-service:1.0-SNAPSHOT (C:\workspace\tools\idea\myproject\mysb\user-service\pom.xml) has 1 error [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 73, column 21 [ERROR] [ERROR] The project org.pw:order-service:1.0-SNAPSHOT (C:\workspace\tools\idea\myproject\mysb\order-service\pom.xml) has 1 error [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 85, column 21 [ERROR] [ERROR] The project org.pw:login-service:1.0-SNAPSHOT (C:\workspace\tools\idea\myproject\mysb\login-service\pom.xml) has 1 error [ERROR] 'dependencies.dependency.version' for org.mybatis.spring.boot:mybatis-spring-boot-starter:jar is missing. @ line 82, column 21 [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles:.
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值