一.创建maven工程
pom.xml
4.0.0 org.mybatis mybatis-demo1 1.0.0 8 8 junit junit 4.12 test mysql mysql-connector-java 5.1.47 org.mybatis mybatis 3.4.6 org.projectlombok lombok 1.18.12 provided
insert into s1(ename,jod_id,mgr,salary,bouns,dept_id) values (#{ename},#{jod_id},#{mgr},#{salary},#{bouns},#{dept_id}) delete from s1 where ename=#{ename};
public class Student {
private String ename;
private int jod_id;
private int mgr;
private double salary;
private double bouns;
private int dept_id;
public Student(String ygl, int i, int i1, double v, double v1, int i2) {
ename=ygl;jod_id=i;mgr=i1;salary=v;bouns=v1;dept_id=i2;
}
}
import org.mybatis.Student.Student;
public interface StudentInterface {
public int insertStudent(Student 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.mybatis.Student.Student;
import java.io.IOException;
import java.io.InputStream;
public class StudentInterfaceTest {
@org.junit.Test
public void testInsertStudent() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");//获取mybatis-config.xml文件
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = builder.build(inputStream);//初始化mybatis,创建SqlSessionFactory类的实例
SqlSession session = sqlSessionFactory.openSession();//创建Session实例
StudentInterface studentInterface = session.getMapper(StudentInterface.class);
int i = studentInterface.insertStudent(new Student("ygl", 1, 6,66666.6, 66666.6, 7));
System.out.println(i);
session.commit();//提交事务
session.close();
}
}
properties属性
这些属性都是可外部配置且可动态替换的,既可以在典型的Java属性文件中配置,亦可以通过properties元素的子元素来传递。
可以在CLASSPATH中增加一个db.properties的Java属性文件。
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/mybatis
username=root
password=root
在配置文件中配置



