Simple implementation of MyBatis

The first step:
Import jar package:
mybatis.jar the sqljdbc.jar
because I use Sql Server so here is sqlj jar
need src package, to add to the class path.
The second step:
the establishment of the entity classes:
One thing to note is the entity class attributes and attribute names in the database need to think of association, that is to say the same.
	private int id;private String name;private int age;

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

public String toString() {
	return this.id+","+this.name+","+this.age;
}

Create a test class:
public class TestMyBatis {

public static void main(String[] args) throws IOException {
	// TODO 自动生成的方法存根
	//加载MyBatis配置文件(为了访问数据库)
	Reader reader = Resources.getResourceAsReader("conf.xml");
	
	SqlSessionFactory sessionFactory =new SqlSessionFactoryBuilder().build(reader);
	//session--connection
	SqlSession session=sessionFactory.openSession();
	String statement="org.awen.entity.personMapper.selectBlog";
	Person student=session.selectOne(statement,1);
	System.out.println(student);
	session.close();

}

Create a file in the package Mapper.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">
 <!-- namespace:该mapper.xml映射文件的 唯一标识 -->
<mapper namespace="org.awen.entity.personMapper">
 <!-- 后续通过namespace:id -->
	 <!-- resultType:查询返回结果值得类型,返回类型 -->
 <select id="selectBlog" resultType="org.awen.entity.Person" parameterType="int">
 <!--sql语句-->
 	select * from person where id = #{id}
 </select>
</mapper>

Establish a config.xml in src, you need to pay attention to the point, the code has been declared before. Carefully watched it.

<!-- 可以再次新建一个environment default="test"
 environment 的id 来指定Mybatis的运行时的数据库环境-->
 <environments default="development">
 <!-- 开发环境(自己的计算机) -->
		 <environment id="development">
		 
		 
		 <!-- 事务的提交方式:
		 	JDBC:利用JDBC方式处理事务(commit roolback close)
		 	MANAGED:将事务交由 其他组件去托管(spring,jobss),默认 会关闭连接
		 	
		 
		  -->
			 <transactionManager type="JDBC"/>
			 <!-- 数据源类型:
			 UNPOOLED:传统的JDBC模式(每次访问数据库,均 需要 打开,关闭等数据库操作 ,但是打开,关闭数据库是非常麻烦的,比较消耗性能)
			 POOLED:使用数据库连接池,也就是说可以进行并发操作,只需要在 最后(也就是说不用的时候关闭上就可以了)
			 JNDI:从tomcat中获取一个内置的数据库连接池
			 
			 
			  -->
				 <dataSource type="POOLED">
					 <!-- 配置数据信息 -->
					 <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
					 <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName =Employees"/>
					 <property name="username" value="sa"/>
					 <property name="password" value="1228"/>
				 </dataSource>
			 </environment>
		 
		 
		  <environment id="test">
				 <transactionManager type="JDBC"/>
				 <dataSource type="POOLED">
				 <!-- 配置数据信息 -->
				 <property name="driver" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
				 <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName =Employees"/>
				 <property name="username" value="sa"/>
				 <property name="password" value="1228"/>
				 </dataSource>
			</environment>
 </environments>
		 
 <mappers>
 <!-- 加载映射文件 -->
 <mapper resource="org/awen/entity/studentMapper.xml"/>
 </mappers>
</configuration>

Create a test file in the package of java

	public static void queryStudenyByStuno() throws IOException {
		// TODO 自动生成的方法存根
		//加载Mybatis配置文件
		//Connection -SqlSession操作Mybatis
		//conf.xml->reader
		Reader reader =Resources.getResourceAsReader("conf.xml");
		//reader -SqlSession
		SqlSessionFactory sessionFacotory=new SqlSessionFactoryBuilder().build(reader);
		SqlSession session=sessionFacotory.openSession();

		String statement="org.awen.entity.studentMapper.queryStudenyByStuno";
		Student student=session.selectOne(statement,1);
		System.out.println(student);
		session.close();		
	}

One thing to note that, Mapper file corresponding to the ID is
String statement = "org.awen.entity.studentMapper." + "QueryStudenyByStuno";
after the plus sign something.

Guess you like

Origin blog.csdn.net/guoguozgw/article/details/91355865