Eclipse Maven mybatis MySQL


1.创建Maven项目,选择Archetype为archetype-maven-quickstart

2.添加mysql依赖

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.6</version>
</dependency>

注意:版本太低会有问题,这里使用5.1.6测试通过

3.mybatis依赖

<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis</artifactId>
    <version>3.5.0</version>
</dependency>

4.创建配置文件

<?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>
    <typeAliases>
        <typeAlias type="com.codingdict.test_mybatis.Person" alias="Person" />
    </typeAliases>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <dataSource type="POOLED">
        <property name="driver" value="com.mysql.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://127.0.0.1:3308/codingdict"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
      </dataSource>
    </environment>
  </environments>
  <mappers>
    <mapper resource="com/codingdict/test_mybatis/PersonMapper.xml" />
  </mappers>
</configuration>

5.创建接口

package com.codingdict.test_mybatis;

public interface IPerson {
    public Integer insertPerson(Person p);
    public Person getPerson(int pid);
}

6.创建实体类

package com.codingdict.test_mybatis;

public class Person {
    public Person() {
        super();
    }
    public Person(int id, String name) {
        super();
        this.id = id;
        this.name = name;
    }
    private int id;
    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;
    }
    private String name;
}

7.创建Mapper映射文件

<?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.codingdict.test_mybatis.IPerson">
    <select id="insertPerson" parameterType="Person" resultType="java.lang.Integer">
       insert into person(id,name) values(#{id},#{name})
    </select>
    <select id="getPerson" resultType="Person" parameterType="java.lang.Integer">
    select * from Person where id=#{id}
    </select>
</mapper>

8.测试

package com.codingdict.test_mybatis;

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

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 App {
    public static void main(String[] args) {
        String resource = "com/codingdict/test_mybatis/config.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
        } catch (IOException e) {
            e.printStackTrace();
        }
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        IPerson personOperation = sqlSession.getMapper(IPerson.class);
        Person p = personOperation.getPerson(1);
        System.out.println(p.getName());
    }
}