1. myBatis目录结构:
--src ---entity [POJO类] ---mappers [映射类] ----*Mapper.java [方法接口,相当于Dao] ----*Mapper.xml [sql语句文件,相当于DaoImpl] ---utils [工具类] ---jdbc.properties [数据库信息文件] ---myBatis-config.xml [myBatis配置文件] --lib [架包文件] ---mybatis-3.2.8.jar ---mysql-connector-java-5.1.7-bin.jar |
2.entity
package com.blueStarWei.entity; public class TPersonInfo { private Integer id;
private String name;
private Integer age; //setter & getter
@Override
public String toString() {
return "TPersonInfo [id=" + id + ", name=" + name + ", age=" + age + "]";
} }
3. mappers
3.1 *Mapper.java
package com.blueStarWei.mappers; import java.util.List; import com.blueStarWei.entity.TPersonInfo; public interface PersonMapper {
/**
*
* @param person
* @return success ? 1 : 0
*/
int create(TPersonInfo person); int update(TPersonInfo person); int delete(Integer id); TPersonInfo findById(Integer id); List<TPersonInfo> findAll();
}
3.2 *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">
<mapper namespace="com.blueStarWei.mappers.PersonMapper"> <!-- parameterType可以省略 -->
<!-- id : 对应方法名 -->
<insert id="create" parameterType="TPersonInfo" >
insert into t_person_info values(null,#{name},#{age})
</insert> <update id="update" parameterType="TPersonInfo">
update t_person_info set name = #{name}, age = #{age} where id = #{id}
</update> <delete id="delete" parameterType="Integer">
delete from t_person_info where id = #{id}
</delete> <select id="findById" parameterType="Integer" resultMap="personResult">
select * from t_person_info a where a.id = #{id}
</select> <select id="findAll" resultMap="personResult">
select * from t_person_info
</select> <resultMap type="TPersonInfo" id="personResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> </mapper>
4. utils
package com.blueStarWei.utils; 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 SqlSessionFactoryUtil { private static SqlSessionFactory sessionFactory; public static SqlSessionFactory getSqlSessionFactory(){
if(sessionFactory == null){
InputStream inputStream;
try {
inputStream = Resources.getResourceAsStream("mybatis-config.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
return sessionFactory;
} public static SqlSession openSession(){
return getSqlSessionFactory().openSession();
}
}
5. jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/db_mybatis
jdbc.username=root
jdbc.password=root
6. mybatis-config.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>
<properties resource="jdbc.properties"/>
<!-- 类型别名:给Java类起一个简单的名字 name使用包名-->
<typeAliases>
<package name="com.blueStarWei.entity"/>
</typeAliases> <!-- 环境信息 -->
<environments default="development">
<environment id="development">
<!-- myBatis支持两种事务管理:JDBC和MANAGED(托管)
MANAGED一般商业服务器才有此功能,例如JBOSS-->
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment>
</environments> <!-- 指定映射的目录 -->
<mappers>
<package name="com.blueStarWei.mappers"/>
</mappers>
</configuration>
6.1 配置文件讲解:
6.1.1 environments : MyBatis 支持多个环境,可以任意配置;
6.1.2 transactionManager : MyBatis 支持两种类型的事务管理器:JDBC 和 MANAGED(托管);
6.1.2.1 JDBC:应用程序负责管理数据库连接的生命周期
6.1.2.2 MANAGED:由应用服务器负责管理数据库连接的生命周期;(一般商业服务器才有此功能,如 JBOSS,WebLogic)
6.1.3 dataSource : 用来配置数据源;类型有:UNPOOLED,POOLED,JNDI;
6.1.3.1 UNPOOLED : 没有连接池,每次数据库操作,MyBatis 都会创建一个新的连接,用完后,关闭;适合小并发项目.
6.1.3.2 POOLED : 连接池,每次数据库操作,MyBatis 都会从连接池中获取链式,可用于高并发项目。【推荐】
6.1.3.3 JNDI,使用应用服务器配置 JNDI 数据源获取数据库连接
7. lib
获取目录:https://pan.baidu.com/s/1248sNRHReiK8S3pENKfmlA 密码:xpe7
更多内容,请访问:http://www.cnblogs.com/BlueStarWei/