一、两种方式初始化Mybatis
2024-08-28 20:15:41
一、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);
最新文章
- 【记录】xUnit for vs2012/vs2013
- iOS 设置1像素的UIView线
- R12.2 URL Validation failed. The error could have been caused through the use of the browser&#39;s navigation buttons
- poj 3264:Balanced Lineup(线段树,经典题)
- 歐洲國家拓展其移動和IT服務業務
- High Aavialability with Group Replication-by宋利兵
- 十款基础级WordPress插件
- RGB颜色查询对照表
- 导航 - 利用系统自带的App导航
- list和数组之间相互的转化
- 关于Linux的缓存内存 Cache Memory详解<;转>;
- Google Code Jam 2012 round 2 problem A: Swinging Wild
- 《Programming WPF》翻译 第6章 2.资源与样式
- C#获取本周、上周、本月、上月、本季度、上季度、本年、上一年起始时间和结束时间
- Monkey源代码分析番外篇WindowManager如何出的喷射事件的进程间的安全限制
- 如何使用jQuery动态的在body里添加script标签?
- js让input失去焦点
- maven项目打包运行出错问题汇总
- 【转】Android-Input 触控笔
- springboot 日志【转】【补】
热门文章
- TDirectory.GetFiles获取指定目录下的文件
- 在windows下的mysql使用
- DSP:CCS V6 TMS320F2812 使用printf函数
- VHDL程序的库
- EF5.0 对一个或多个实体的验证失败。有关详细信息,请参见“EntityValidationErrors”属性
- 国内物联网平台初探(八):移动云OneNET
- caffe之(三)激活函数层
- Cocos2d-x win7下 android环境搭建
- ANDROID_MARS学习笔记_S03_004_getAllProviders、LOCATIONLISTENER、getBestProvider
- message 匹配不上grok正则 也会写入到elasticsearch