1、MAVEN实现包导入
<!-- 导入Mysql数据库链接jar包 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.36</version> </dependency> <!-- mybatis核心包 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.3.0</version> </dependency>
2、mybatis.cfg.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> <!--定义连接池--> <environments default="development"> <environment id="development"> <transactionManager type="jdbc"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://***********:3306/***"/> <property name="username" value="***"/> <property name="password" value="******"/> </dataSource> </environment> </environments> <!--配置每张表的映射文件--> <mappers> <mapper resource="member.xml"/> </mappers> </configuration>
3、member.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="Member"> <insert id="create" parameterType="com.vitoh.mybatis.dao.Member"> insert into member(name,age,sex) values (#{name},#{age},#{sex}) </insert> </mapper>
4、Member.java
package com.vitoh.mybatis.dao; import java.io.Serializable; /** * Created by vitohuang on 15/10/19. */ public class Member implements Serializable{ private Long id; private String name; private Integer sex; private Integer age; public Member() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
5、测试Demo
package com.vitoh.mybatis.test; import com.vitoh.mybatis.dao.Member; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import java.io.Reader; /** * Created by vitohuang on 15/10/13. */ public class TestOne { private static SqlSessionFactory sqlSessionFactory; private static Reader reader; private static final String CONFIG = "mybatis.cfg.xml"; static{ try{ //第一步读取配置文件信息 reader = Resources.getResourceAsReader(CONFIG); //第二步通过 SqlSessionFactoryBuilder 实例化 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader); }catch(Exception e){ e.printStackTrace(); } } public static void main(String args[]){ //第三步获取操作数据库对象SqlSession SqlSession session = sqlSessionFactory.openSession(); try { Member user = new Member(); user.setName("VitoHuang"); user.setAge(24); user.setSex(0); //第四步CRUD session.insert("Member.create",user); //第五步提交事务 session.commit(); }catch (Exception e){ session.rollback(); e.printStackTrace(); } finally { //第六步关闭连接 session.close(); } } }
时间: 2024-10-07 08:25:13