目录 start
目录 end
|2018-04-18| 码云 | CSDN | OSChina
一个灵活的数据库中间件框架
<?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="cn.mybatis.test.Human" alias="Human" />
</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://localhost:3306/test?characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 配置mappers -->
<mappers>
<mapper resource="cn/mybatis/test/HumanDao.xml" />
</mappers>
</configuration>
<?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="cn.mybatis.test">
<!-- 按id查询 -->
<select id="queryUsersById" parameterType="Human" resultType="Human">
<!-- useCache="false" -->
<![CDATA[
select * from inserts t where t.id=#{id}
]]>
</select>
<!-- 查询全部 -->
<select id="queryUsers" resultType="Human">
select * from inserts
</select>
<!-- 插入记录 -->
<insert id="insertUser" parameterType="Human" >
<!-- 该字段是必须要在数据库中自增长的
可能会有并发问题
useGeneratedKeys="true" keyProperty="id"
所以用查询方式好点, 写语句就不要考虑主键了
-->
<selectKey resultType="int" keyProperty="id">
select LAST_INSERT_ID()
</selectKey>
insert into inserts (name) values(#{name})
</insert>
<!-- 删除记录 -->
<delete id="deleteUser" parameterType="String">
delete from inserts where id=#{id}
</delete>
<!-- 更新记录 -->
<update id="updateUserById" parameterType="Human">
update inserts set name=#{name} where id=#{id}
</update>
</mapper>
private static SqlSessionFactory sessionFactory;
static{
try{
String resource = "cn/mybatis/test/mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}catch (Exception e) {
System.out.println("获取Session失败");
}
}
/**
* 获取Session
*/
public static SqlSession getSession(){
SqlSession session = null;
session = sessionFactory.openSession();
return session;
}
<!--基本属性-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driver}"/>
<property name="jdbcUrl" value="${url}"/>
<property name="user" value="${username}"/>
<property name="password" value="${password}"/>
<property name="initialPoolSize" value="${initialSize}"/>
<property name="maxPoolSize" value="${maxSize}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--操作配置文件的路径-->
<property name="mapperLocations" value="classpath:bean/*.xml"/>
<!--bean的路径,进行别名的自动扫描-->
<property name="typeAliasesPackage" value="com.book.bean"/>
</bean>
<bean id="mybatisSessionFactory" class="com.book.dao.MybatisSessionFactory">
<property name="sessionFactory" ref="sqlSessionFactory"/>
</bean>
<!--定义数据源-->
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
@Component
public class MybatisSessionFactory {
@Autowired
private SqlSessionFactory sessionFactory;
// 日志
private static org.slf4j.Logger Log = LoggerFactory.getLogger(MybatisSessionFactory.class);
//使用本地线程组能避免不必要的Session开支,加强性能
private static final ThreadLocal<SqlSession> THREAD_LOCAL = new ThreadLocal<SqlSession>();
/**
* 获取Session
* @return
*/
public SqlSession getSession(){
SqlSession session = (SqlSession)THREAD_LOCAL.get();
if(session==null ){
session = this.sessionFactory.openSession();
THREAD_LOCAL.set(session);
}
Log.info("__获取了一个Session__"+session);
return session;
}
/*
关闭连接
*/
public void closeSession(){
SqlSession session = (SqlSession)THREAD_LOCAL.get();
THREAD_LOCAL.set(null);
if(session!=null){
session.close();
}
}
public SqlSessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SqlSessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
}
<foreach collection="param_list 自定义的话就是Map中的key,或者使用 @Param("")来指定 " item="params" index="currentIndex 当前索引" separator="循环分隔符" open="在循环前加上字符" close="循环结束后加上字符">
${params}
</foreach>
<if test=""></if>
<set><if test="col!=null">col=#{col},</if></set>
mybatis会自动去除多余的逗号,但是每一行书写要写逗号
<choose><when test=""></when></choose>