Mybatis入门之数据库中插入数据(eclipse版)

Mybatis知识储备:

一.框架
理解:framework其实就是某种应用的半成品,就是一组组件,供你选用完成你自己的系统。简单说就是使用别人搭好的舞台,你来做表演。而且,框架一般是成熟的,不断升级的软件
框架的优点:
1、代码模板化
2、重用
3、高内聚(封装)
4、规范
5、可扩
6、可维护
7、协作开发
8、通用性
如何使用框架?
jar包(类库)+配置文件(.xml/.properties)或注解

二、Mybatis与jdbc关系
Mybatis是一个开源的持久层(操控库表技术与jdbc是同样功能)框架。
Mybatis是在jdbc的基础之上封装而成的持久层框架。
Mybatis是一个ORM框架。ORM(object relational mapping):对象关系型映射
Mybatis入门之数据库中插入数据(eclipse版)_第1张图片

Mybatis入门之数据库中插入数据(eclipse版)_第2张图片

项目结构如下:

Mybatis入门之数据库中插入数据(eclipse版)_第3张图片

数据库建表:

Mybatis入门之数据库中插入数据(eclipse版)_第4张图片

准备工作:

一、配置好maven
window---->preferences—>maven
Mybatis入门之数据库中插入数据(eclipse版)_第5张图片
Mybatis入门之数据库中插入数据(eclipse版)_第6张图片
二、创建无骨架maven工程
file—>new —>maven project
Mybatis入门之数据库中插入数据(eclipse版)_第7张图片
三、pom.xml配置
pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0modelVersion>
	<groupId>com.henu.mybatisgroupId>
	<artifactId>mybatis01artifactId>
	<version>0.0.1-SNAPSHOTversion>
	
	<properties>
		<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
		<maven.compiler.source>1.8maven.compiler.source>
		<maven.compiler.target>1.8maven.compiler.target>
	properties>
	<dependencies>
		
		<dependency>
			<groupId>mysqlgroupId>
			<artifactId>mysql-connector-javaartifactId>
			<version>5.1.39version>
		dependency>
		
		<dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.5version>
        dependency>
	dependencies>
project>

正式开始:

文件的配置
Mybatis入门之数据库中插入数据(eclipse版)_第8张图片
mybatisConfig.xml(配置文件)




<configuration>
	
	<environments default="mysql">
		<environment id="mysql">
			
			<transactionManager type="JDBC">transactionManager>
			
			<dataSource type="POOLED">
				
				<property name="driver" value="com.mysql.jdbc.Driver" />
				<property name="url" value="jdbc:mysql://localhost:3306/mybatis0910" />
				<property name="username" value="root" />
				<property name="password" value="123456" />
			dataSource>
		environment>
	environments>
	
	<mappers>
	<mapper resource="mapper/UserMapper.xml">mapper>
	mappers>
configuration>

Mybatis入门之数据库中插入数据(eclipse版)_第9张图片
创建mapper文件夹,在下面创建UserMapper.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.henu.mybatis.bean">
        	<!--向数据库中添加数据  -->
        	<insert id="insertUser">
        	insert into user values(1,'admin','admin',18);
        	</insert>
        </mapper>	

一:创建实体类

Mybatis入门之数据库中插入数据(eclipse版)_第10张图片

package com.henu.mybatis.bean;
/**
 * 用户实体类
 * @author YAN
 *
 */
public class User {
	private Integer id;
	private String uname;
	private String upass;
	private Integer uage;
	public Integer getUage() {
		return uage;
	}
	public void setUage(Integer uage) {
		this.uage = uage;
	}
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}
	public String getUpass() {
		return upass;
	}
	public void setUpass(String upass) {
		this.upass = upass;
	}
	
	public User(Integer id, String uname, String upass, Integer uage) {
		super();
		this.id = id;
		this.uname = uname;
		this.upass = upass;
		this.uage = uage;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", uname=" + uname + ", upass=" + upass + ", uage=" + uage + "]";
	}
	public User() {
		super();
	}
	
}

二、创建测试类
Mybatis入门之数据库中插入数据(eclipse版)_第11张图片
UserTest.java

package com.henu.mybatis.text;

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

import javax.annotation.Resource;

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 UserTest {

	public static void main(String[] args) throws Exception {
		//4.加载配置文件
		Reader reader =  Resources.getResourceAsReader("mybatisConfig.xml");
		//3.创建构造器对象
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
		//2.创建工厂
		SqlSessionFactory sqlSessionFactory = builder.build(reader);
		//1.创建执行对象
		SqlSession session = sqlSessionFactory.openSession();
		//执行sql语句
		session.insert("insertUser");
		//提交事务
		session.commit();
		//关闭资源
		session.close();
	}

}

本文中mybatis配置文件xml的约束dtd,须能看懂
Mybatis入门之数据库中插入数据(eclipse版)_第12张图片



<!ELEMENT configuration (properties?, settings?, typeAliases?, typeHandlers?, objectFactory?, objectWrapperFactory?, reflectorFactory?, plugins?, environments?, databaseIdProvider?, mappers?)>

<!ELEMENT databaseIdProvider (property*)>
<!ATTLIST databaseIdProvider
type CDATA #REQUIRED
>

<!ELEMENT properties (property*)>
<!ATTLIST properties
resource CDATA #IMPLIED
url CDATA #IMPLIED
>

<!ELEMENT property EMPTY>
<!ATTLIST property
name CDATA #REQUIRED
value CDATA #REQUIRED
>

<!ELEMENT settings (setting+)>

<!ELEMENT setting EMPTY>
<!ATTLIST setting
name CDATA #REQUIRED
value CDATA #REQUIRED
>

<!ELEMENT typeAliases (typeAlias*,package*)>

<!ELEMENT typeAlias EMPTY>
<!ATTLIST typeAlias
type CDATA #REQUIRED
alias CDATA #IMPLIED
>

<!ELEMENT typeHandlers (typeHandler*,package*)>

<!ELEMENT typeHandler EMPTY>
<!ATTLIST typeHandler
javaType CDATA #IMPLIED
jdbcType CDATA #IMPLIED
handler CDATA #REQUIRED
>

<!ELEMENT objectFactory (property*)>
<!ATTLIST objectFactory
type CDATA #REQUIRED
>

<!ELEMENT objectWrapperFactory EMPTY>
<!ATTLIST objectWrapperFactory
type CDATA #REQUIRED
>

<!ELEMENT reflectorFactory EMPTY>
<!ATTLIST reflectorFactory
type CDATA #REQUIRED
>

<!ELEMENT plugins (plugin+)>

<!ELEMENT plugin (property*)>
<!ATTLIST plugin
interceptor CDATA #REQUIRED
>

<!ELEMENT environments (environment+)>
<!ATTLIST environments
default CDATA #REQUIRED
>

<!ELEMENT environment (transactionManager,dataSource)>
<!ATTLIST environment
id CDATA #REQUIRED
>

<!ELEMENT transactionManager (property*)>
<!ATTLIST transactionManager
type CDATA #REQUIRED
>

<!ELEMENT dataSource (property*)>
<!ATTLIST dataSource
type CDATA #REQUIRED
>

<!ELEMENT mappers (mapper*,package*)>

<!ELEMENT mapper EMPTY>
<!ATTLIST mapper
resource CDATA #IMPLIED
url CDATA #IMPLIED
class CDATA #IMPLIED
>

<!ELEMENT package EMPTY>
<!ATTLIST package
name CDATA #REQUIRED
>

你可能感兴趣的:(SSM框架)