輕鬆整合Mybatis框架
阿新 • • 發佈:2019-02-05
眾所周知,Mybatis是一個JAVA輕量級(相對於Hibernate與)的資料庫處理框架,支援xml對映檔案、動態SQL。對於習慣了寫複雜SQL語句的開發人員來說,是非常方便的,他摒除了jdbc的冗長寫法,極大地減少了程式碼行數。不同於Hibernate繁瑣的配置,Mybatis的整合是非常容易的。
1.新建新建實體類:
public Class User{
public String user_id;
public String user_name;
}
2.新建對映介面:com.company.mapInterface.UserMapper
public interface UserMapper {
public User findByUserId(String userId);
}
3.新建xml對映檔案com/company/mappers/UserMapping.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">
<!--這塊等於dao介面的實現 namespace必須和介面的類路徑一樣-->
<mapper namespace="com.company.mapInterface.UserMapper">
<!-- findByUserId必須和介面中的方法名一樣 返回一個User就是剛才的別名-->
<select id="findByUserId" parameterType="HashMap" resultType="User">
select * from user_data where user_id=#{userId}
</select>
</mapper>
4.在com.company.config下新建batis配置檔案mybatis.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>
<typeAliases>
<!—作用:該包下所有實體類自動使用別名 -->
<package name="com.company.bean"/>
</typeAliases>
<!--資料來源配置 使用mysql資料庫 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<!--Mysql寫法-->
<!-- <property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> -->
<!--Oracle寫法-->
<property name="driver" value="oracle.jdbc.OracleDriver"></property>
<property name=" url"value="jdbc:oracle:thin:@XXX"></property>
<!--賬號/密碼-->
<property name="username" value="test" />
<property name="password" value="psw" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml裝載進來 同等於把“dao”的實現裝載進來 -->
<mapper resource="com/company/mappers/UserMapping.xml" />
</mappers>
</configuration>
其中dataSource 的配置也可以使用佔位符,如下:
<?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>
<!—作用:該包下所有實體類自動使用別名 -->
<package name="com.company.bean"/>
</typeAliases>
<properties resource="org/mybatis/example/config.properties">
<property name="driver" value="oracle.jdbc.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@XXX"/>
<property name="username" value="test"/>
<property name="password" value="psw"/>
</properties>
<!--資料來源配置 使用mysql資料庫 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<!--Oracle寫法-->
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<!--賬號/密碼-->
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml裝載進來 同等於把“dao”的實現裝載進來 -->
<mapper resource="com/company/mappers/UserMapping.xml" />
</mappers>
</configuration>
還可以使用配置檔案,如下:
<?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>
<!—作用:該包下所有實體類自動使用別名 -->
<package name="com.company.bean"/>
</typeAliases>
<properties resource="jdbc.properties"></properties>
<!--資料來源配置 使用mysql資料庫 -->
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<!--Mysql寫法-->
<!-- <property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis" /> -->
<!--Oracle寫法-->
<property name="driver" value="${driver}"></property>
<property name="url" value="${url}"></property>
<!--賬號/密碼-->
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</dataSource>
</environment>
</environments>
<mappers>
<!--userMapper.xml裝載進來 同等於把“dao”的實現裝載進來 -->
<mapper resource="com/company/mappers/UserMapping.xml" />
</mappers>
</configuration>
然後同路徑下新建jdbc.properties檔案
driver=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@XXX
username=test
password=psw
5.最後一步,測試:
(1)新建工具類:
public class BatisUtil {
public static SqlSessionFactory getSessionFactory() {
SqlSessionFactory sessionFactory = null;
String resource = "com/company/config/mybatis.xml";
try {
sessionFactory = new SqlSessionFactoryBuilder().build(Resources
.getResourceAsReader(resource));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return sessionFactory;
(2)新建測試類:
public class MyBatisTest {
public static void main(String[] args) {
SqlSession sqlSession = BatisUtil.getSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.findByUserId("1455");
System.out.println(user.user_name);
}
}
執行,正確打印出資料庫中的user_name值,完成。