一、两种方式初始化Mybatis
一、xml
Configuration.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="config/config.properties">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="asd123" />
</properties> <!-- 可以不设置 -->
<!-- <setting></setting> --> <typeAliases>
<typeAlias alias="Student" type="entity.Student"></typeAlias>
<typeAlias alias="Score" type="entity.Score"></typeAlias>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/StudentMapper.xml" />
<mapper resource="entity/ScoreMapper.xml" />
</mappers>
</configuration>
StudentMapper.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="mapper.StudentMapper"> <resultMap type="Student" id="StudentResultMap">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="password" column="password" />
<result property="sex" column="sex" />
<result property="college" column="college" />
<collection property="scores" column="sid" javaType="ArrayList" ofType="Score" resultMap="Score.ScoreResultMap" />
</resultMap> <select id="get" parameterType="String" resultMap="StudentResultMap">
SELECT * FROM sc_student st LEFT OUTER JOIN sc_score sc ON st.id=sc.sid WHERE st.id=#{id}
</select>
<insert id="insert" parameterType="Student">
insert into sc_student (id,name,password,sex,college) values
(#{id},#{name},#{password},#{sex},#{college})
</insert>
<update id="update" parameterType="Student">
UPDATE sc_student SET id=#{id},name=#{name},password=#{password},
sex=#{sex},college=#{college} WHERE id=#{id}
</update>
<delete id="delete" parameterType="Student">
DELETE FROM sc_student WHERE id = #{id}
</delete>
</mapper>
package test; import java.io.IOException;
import java.io.Reader; import mapper.StudentMapper; 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.Before;
import org.junit.BeforeClass;
import org.junit.Test; import entity.Student; public class TestXmlBuild1 {
public static SqlSessionFactory sqlMapper = null; @Before
public void setUpClass() throws IOException {
String resource = "config/Configuration1.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMapper = new SqlSessionFactoryBuilder().build(reader);
} @Test
public void test1() {
SqlSession session = sqlMapper.openSession();
//第一种方法,直接调用,名字要和<mapper namespace="mapper.StudentMapper">匹配
try {
Student student = (Student) session.selectOne("mapper.StudentMapper.get", "ttt");
System.out.println(student);
} finally {
session.close();
}
} @Test
public void test2() {
SqlSession session = sqlMapper.openSession();
//第二种方法,通过接口,mapper名字需要和<mapper namespace="mapper.StudentMapper">匹配
try {
StudentMapper studentMapper = session.getMapper(StudentMapper.class);
Student student = studentMapper.get("ttt123");
System.out.println(student);
} finally {
session.close();
}
}
}
二、不使用XML构建SqlSessionFactory
没用到,就不解释了。
DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();
TransactionFactory transactionFactory = new
JdbcTransactionFactory();
Environment environment =
new Environment("development", transactionFactory, dataSource);
Configuration configuration = new Configuration(environment);
configuration.addMapper(BlogMapper.class);
SqlSessionFactory sqlSessionFactory =
new SqlSessionFactoryBuilder().build(configuration);
一、两种方式初始化Mybatis的更多相关文章
- easyUI 初始化的两种方式
easyUI 初始化的两种方式: class方式和js方式: <!DOCTYPE html> <html lang="en"> <head> & ...
- 使用GoldenGate初始化的两种方式
在使用OGG开始增量数据的实时复制之前,一般需要对当前的存量数据进行初始化,如果是同构数据库,则可以使用数据库自带的工具完成,比如Oracle DB中的rman, expdp/impdp等. 其实og ...
- MyBatis开发Dao层的两种方式(原始Dao层开发)
本文将介绍使用框架mybatis开发原始Dao层来对一个对数据库进行增删改查的案例. Mapper动态代理开发Dao层请阅读我的下一篇博客:MyBatis开发Dao层的两种方式(Mapper动态代理方 ...
- MyBatis开发Dao层的两种方式(Mapper动态代理方式)
MyBatis开发原始Dao层请阅读我的上一篇博客:MyBatis开发Dao层的两种方式(原始Dao层开发) 接上一篇博客继续介绍MyBatis开发Dao层的第二种方式:Mapper动态代理方式 Ma ...
- springboot整合mybatis的两种方式
https://blog.csdn.net/qq_32719003/article/details/72123917 springboot通过java bean集成通用mapper的两种方式 前言:公 ...
- MyBatis配置数据源的两种方式
---------------------siwuxie095 MyBatis 配置数据源的两种方式 1.配置方 ...
- 设置Mybatis打印调试sql的两种方式
http://blog.csdn.net/gao36951/article/details/53641432 ********************************************* ...
- mybatis批量保存的两种方式(高效插入)
知识点:mybatis中,批量保存的两种方式 1.使用mybatis foreach标签 2.mybatis ExecutorType.BATCH 参考博客:https://www.jb51.net/ ...
- JavaWeb应用中初始化Log4j的两种方式
本文主要介绍了普通JavaWeb应用(基于Tomcat)中初始化Log4j的两种方式: 1.通过增加 InitServlet ,设置令其自启动来初始化 Log4j . 2.通过监听器 ServletC ...
随机推荐
- 前端工程的构建工具对比 Gulp vs Grunt
1. Grunt -> Gulp 早些年提到构建工具,难免会让人联想到历史比较悠久的Make,Ant,以及后来为了更方便的构建结构类似的Java项目而出现的Maven.Node催生了一批自动化工 ...
- aspose.cells根据模板导出excel
又隔十多天没写博客了,最近都在忙项目的事情,公司人事变动也比较大,手头上就又多了一个项目.最近做用aspose.cells根据模板导出excel报价单的功能,顺便把相关的核心记下来,先上模板和导出的效 ...
- WebViewJavascriptBridge
上一篇文章介绍了通过UIWebView实现了OC与JS交互的可能性及实现的原理,并且简单的实现了一个小的示例DEMO,当然也有一部分遗留问题,使用原生实现过程比较繁琐,代码难以维护.这篇文章主要介绍下 ...
- aspx、ashx以及cs的关系,viewState
aspx和ashx关系:aspx就是一种特殊的ashx,aspx对应的类是page,它是实现了IHttpHandler接口,所以说aspx是高级的HttpHandler.aspx中帮我们封装了很多操作 ...
- JNI/NDK开发指南(开山篇)
转载请注明出处:http://blog.csdn.net/xyang81/article/details/41759643 相信很多做过Java或Android开发的朋友经常会接触到JNI方面的技术, ...
- 在C#里实现各种窗口切换特效,多达13种特效
原文:http://www.cnblogs.com/clayui/archive/2011/06/28/2092126.html 预览: 下载 这次clayui给大家带来了比较实用的东西,因为时间 ...
- kav卡巴斯基2014 优化设置
- WordPress 全方位优化指南(上)
作为一个全面的 WordPress 性能优化教程,本文旨在帮助读者排查 WordPress 网站的性能问题,同时也提供网站前端优化加速的建议. 如果你曾经遇到过 WordPress 管理界面加载缓慢. ...
- File上传
http://wenku.baidu.com/link?url=bsufrjDQztceWCUw2gqFeYkL1_95t9yIyV1LfHVYL9URAlpWSK_ALO1bAWSlBdnPXYXE ...
- Oracle的常见错误及解决办法
ORA-12528: TNS:listener: all appropriate instances are blocking new connections ORA-12528问题是因为监听中的服务 ...