java 學習筆記--mybatis 三劍客(mybatis)
阿新 • • 發佈:2018-02-06
pac 筆記 pat 支持 div myba param oid 方法
Java項目中使用Mybatis入門程序
wanna 關註
2017.03.23 14:33* 字數 270 閱讀 1243評論 0喜歡 5
MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的持久層框架。
歡迎訪問本人博客:http://wangnan.tech
什麽是 MyBatis ?
MyBatis 是支持定制化 SQL、存儲過程以及高級映射的優秀的持久層框架。
MyBatis github
https://github.com/mybatis/mybatis-3
MyBatis 文檔
http://mybatis.github.io/mybatis-3/zh/index.html
入門程序
想要使用 MyBatis 只需將 mybatis-x.x.x.jar 文件置於 classpath 中。
如果使用 Maven 構建項目,則需將下面的 dependency 置於 pom.xml 中:
<dependency>
<groupId>
org.mybatis
</groupId>
<artifactId>
mybatis
</artifactId>
<version>
3.8.2
</version>
</dependency>
我選擇第二種,在Eclipse中新建一個maven項目
項目結構:
01.jpg
各文件介紹:
- pom文件:由於使用了mySQL數據庫,這裏還依賴了一個mySQL驅動包
- 實體類User :
- UserMapper接口:
- UserMapper接口的實現,userMapper.xml:
- 數據源配置 configuration.xml
- 測試類MyBatisTest:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>
4.0.0
</modelVersion>
<groupId>
Mybatis
</groupId>
<artifactId>
Mybatis
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
<dependencies>
<dependency>
<groupId>
org.mybatis
</groupId>
<artifactId>
mybatis
</artifactId>
<version>
3.2.8
</version>
</dependency>
<dependency>
<groupId>
mysql
</groupId>
<artifactId>
mysql-connector-java
</artifactId>
<version>
5.1.34
</version>
</dependency>
</dependencies>
</project>
package
com.mybatis.domain;
public
class User
{
private
String name;
private
Integer age;
public String getName()
{
return
name;
}
public void setName(String name)
{
this
.name = name;
}
public Integer getAge()
{
return
age;
}
public void setAge(Integer age)
{
this
.age = age;
}
}
package com.mybatis.mapper;
import com.mybatis.domain.User;
publicinterface UserMapper
{
public User findById(String Id)
;
}
<?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">
<!--相當於UserMapper接口的實現 namespace必須是UserMapper類路徑-->
<mapper namespace="com.mybatis.mapper.UserMapper">
<!-- findById必須和接口中的方法名一樣 返回一個User-->
<select id="findById" parameterType="String"
resultType="com.mybatis.domain.User">
select * from user where id=#{id}
</select>
</mapper>
<?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>
<!--數據源配置 -->
<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" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml裝載進來 -->
<mapper resource="userMapper.xml" />
</mappers>
</configuration>
package com.mybatis.test;
import com.mybatis.domain.User;
import com.mybatis.mapper.UserMapper;
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 java.io.IOException;
publicclass MyBatisTest
{
/** * MyBatis SqlSessionFactory * SqlSessionFactory????SqlSession????????????SqlSession??????????commit?rollback?close???? * @return */
private static SqlSessionFactory getSessionFactory()
{
SqlSessionFactory sessionFactory =
null;
String resource =
"configuration.xml";
try
{
sessionFactory =
newSqlSessionFactoryBuilder().build(Resources.getResourceAsReader(
resource));
}
catch(IOException e) {
e.printStackTrace();
}
return
sessionFactory;
}
public static void main(String[] args)
{
SqlSession sqlSession = getSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findById(
"1");
System.out.println(user.getName());
}
}
數據庫中插入條數據:
id=”1” name=”wn” age=”23”
java 學習筆記--mybatis 三劍客(mybatis)