第一步创建一个工程选择maven创建一个新的maven工程
依次创建如图所示的包,并添加TestDao接口与TestDao.xml
同方法创建domain与until包分别创建类student;MyApp。
resources文件下创建mybatis.xml
创建一个test文件夹并建立相应的包存放testmybatis类其中java文件夹转为test sources root
testdao接口中写入
package com.hxx.dao;
import com.hxx.domain.Student;
import java.util.List;
//接口 操作test表
public interface TestDao {
public List<Student> selectStudent();
public int insertStudent(String student);
public int updateStudent(String student);
public int deleteStudent(String student);
}
testdao.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.hxx.dao.TestDao">
<select id="selectStudent" resultType="com.hxx.domain.Student">
select id,name,password from test
</select>
<insert id="insertStudent">
insert into test values(#{id},#{name},#{password})
</insert>
<update id="updateStudent">
update test set name=#{name},password=#{password} where id=#{id}
</update>
<delete id="deleteStudent">
delete from test where id=#{id}
</delete>
</mapper>
domain包下Student类中写入
package com.hxx.domain;
public class Student {
private int id;
private String name;
private String password;
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;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
MyApp类中写入
package com.hxx;
import com.hxx.domain.Student;
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.IOException;
import java.io.InputStream;
import java.util.List;
public class MyApp {
public static void main(String[] args) throws IOException {
/**String config="mybatis.xml";
InputStream in=Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao"+"."+"selectStudent";
List<Student> studentList=sqlSession.selectList(sqlId);
for(Student stu:studentList){
System.out.println("查询结果为:"+stu);
}
sqlSession.close();
**/
/** String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao.insertStudent";
Student student=new Student();
student.setId(1);
student.setName("张三");
student.setPassword("321");
int nums=sqlSession.insert(sqlId,student);
sqlSession.commit();
System.out.println("插入结果为="+nums);
sqlSession.close();**/
/**String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao.updateStudent";
Student student=new Student();
student.setId(2);
student.setName("朱某");
student.setPassword("666");
int nums=sqlSession.update(sqlId,student);
sqlSession.commit();
System.out.println("修改结果="+nums);
sqlSession.close();**/
/** String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao.deleteStudent";
Student student=new Student();
student.setId(4);
int nums=sqlSession.delete(sqlId,student);
sqlSession.commit();
System.out.println("删除结果="+nums);
sqlSession.close();
**/
}
}
mybatis.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="mysql">
<environment id="mysql">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/student?characterEncoding=utf8"/>
<property name="username" value="root"/>
<property name="password" value=""/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/hxx/dao/TestDao.xml"/>
</mappers>
</configuration>
TestMybatis测试类中写入
package com.hxx;
import com.hxx.domain.Student;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestMybatis {
@Test
public void testselect() throws IOException {
String config="mybatis.xml";
InputStream in=Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao"+"."+"selectStudent";
List<Student> studentList=sqlSession.selectList(sqlId);
for(Student stu:studentList){
System.out.println("查询结果为:"+stu);
}
sqlSession.close();
}
@Test
public void testinsert() throws IOException {
String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao.insertStudent";
Student student=new Student();
student.setId(2);
student.setName("猪猪侠");
student.setPassword("777");
int nums=sqlSession.insert(sqlId,student);
sqlSession.commit();
System.out.println("插入结果为="+nums);
sqlSession.close();
}
@Test
public void testupdate() throws IOException {
String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao.updateStudent";
Student student=new Student();
student.setId(2);
student.setName("奥特曼");
student.setPassword("666");
int nums=sqlSession.update(sqlId,student);
sqlSession.commit();
System.out.println("修改结果="+nums);
sqlSession.close();
}
@Test
public void testdelete() throws IOException {
String config="mybatis.xml";
InputStream in= Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder();
SqlSessionFactory factory=builder.build(in);
SqlSession sqlSession=factory.openSession();
String sqlId="com.hxx.dao.TestDao.deleteStudent";
Student student=new Student();
student.setId(2);
int nums=sqlSession.delete(sqlId,student);
sqlSession.commit();
System.out.println("删除结果="+nums);
sqlSession.close();
}
}
最后于pom.xml中
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>1</groupId>
<artifactId>ch01-hello-mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.10</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties..xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
至此完成整个程序!