Mybatis妗嗘灦 鍏ラ棬瀛︿範
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis妗嗘灦 鍏ラ棬瀛︿範相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/%e5%85%b3%e6%b3%a8' title='鍏虫敞'>鍏虫敞
-o springmvc ref ODB gets class鐩綍
Mybatis妗嗘灦绠€浠?/h1>
妗嗘灦浠嬬粛
浠€涔堟槸妗嗘灦锛屼负浠€涔堜娇?妗嗘灦锛屾鏋朵紭鐐?nbsp;?
妗嗘灦锛?nbsp;framework 锛夋槸?涓熀鏈蹇典笂鐨勭粨鏋勶紝?浜庡幓瑙e喅鎴栬€呭鐞嗗鏉傜殑闂銆?/p>
妗嗘灦锛屽嵆framework銆傚叾瀹炲氨鏄煇绉嶅簲?鐨勫崐鎴愬搧锛屽氨鏄?缁勭粍浠讹紝渚涗綘閫?瀹屾垚浣??鐨勭郴缁熴€傜畝鍗曡灏辨槸浣?鍒?鎼ソ鐨勮垶鍙帮紝浣犳潵鍋氳〃婕斻€?妗嗘灦鏄湪鐗瑰畾鐨勯鍩熷唴瑙e喅闂銆?/p>
浼樼偣 :
1). 閲?浠g爜??澧炲姞锛岃蒋浠?浜ф晥鐜囧拰璐ㄩ噺涔熷緱鍒颁簡鎻?銆?/p>
2). 浣?妗嗘灦寮€鍙戯紝瀹冩彁渚涚粺?鐨勬爣鍑嗭紝??闄嶄綆浜嗘垜浠殑鍚庢湡缁存姢銆?/p>
java 寮€鍙戜腑甯?妗嗘灦 :
1). springmvc spring mybatis(ibatis) -- SSM
2). SpringData SpringBoot SpringCloud
SSM妗嗘灦鍦↗avaEE寮€鍙戜腑鎵€澶勭殑浣嶇疆锛?/p>
鍘熷JDBC鎿嶄綔鏌ヨ
-
@Test
-
public void testQuery(){
-
Connection connection = null;
-
PreparedStatement preparedStatement = null;
-
ResultSet resultSet = null;
-
try {
-
//鍔犺浇鏁版嵁搴撻┍鍔?/code>
-
Class.forName("com.mysql.cj.jdbc.Driver");
-
//閫氳繃椹卞姩绠$悊绫昏幏鍙栨暟鎹簱閾炬帴
-
connection =DriverManager.getConnection("jdbc:mysql://localhost:3306/test_wensong","root", "root");
-
//瀹氫箟sql璇彞 ? 琛ㄧず鍗犱綅绗?/code>
-
String sql = " select * from user where username = ?";
-
//鑾峰彇棰勫鐞唖tatement
-
preparedStatement = connection.prepareStatement(sql);
-
//璁剧疆鍙傛暟锛岀?涓弬鏁颁负sql璇彞涓弬鏁扮殑搴忓彿锛堜粠1寮€濮嬶級锛岀?涓弬鏁颁负璁剧疆鐨勫弬鏁板€?/code>
-
preparedStatement.setString(1, "Tom");
-
//鍚戞暟鎹簱鍙戝嚭sql鎵?鏌ヨ锛屾煡璇㈠嚭缁撴灉闆?/code>
-
resultSet = preparedStatement.executeQuery();
-
List<User> userList = new ArrayList<User>();
-
//閬嶅巻鏌ヨ缁撴灉闆?/code>
-
while(resultSet.next()){
-
User user = new User();
-
user.setId(resultSet.getInteger("id"));
-
user.setUsername(resultSet.getString("username"))
-
user.setSex(resultSet.getString("sex"));
-
user.setAddress(resultSet.getString("address"));
-
userList.add(user);
-
}
-
} catch (Exception e) {
-
e.printStackTrace();
-
}finally{
-
//閲婃斁璧勬簮
-
if(resultSet!=null){
-
try {
-
resultSet.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
if(preparedStatement!=null){
-
try {
-
preparedStatement.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
if(connection!=null){
-
try {
-
connection.close();
-
} catch (SQLException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
鍘熷JDBC鎿嶄綔闂鍒嗘瀽
鍘熷 jdbc 寮€鍙戝瓨鍦ㄧ殑闂濡備笅锛?/p>
鈶?nbsp;. 鏁版嵁搴撹繛鎺ュ垱寤恒€侀噴鏀鹃绻侀€犳垚绯荤粺璧勬簮娴垂浠?褰卞搷绯荤粺鎬ц兘 銆?/p>
鈶?nbsp;. sql 璇彞鍦ㄤ唬鐮佷腑纭紪鐮侊紝閫犳垚浠g爜涓嶆槗缁存姢锛屽疄闄呭簲? sql 鍙樺寲鐨勫彲鑳借緝?锛?nbsp;sql 鍙樺姩闇€瑕佹敼鍙?java 浠g爜 銆?/p>
鈶?nbsp;. 鏌ヨ鎿嶄綔鏃堕渶瑕?鍔ㄥ皢缁撴灉闆嗕腑鐨勬暟鎹?鍔ㄥ皝瑁呭埌瀹炰綋涓€傛彃?鎿嶄綔鏃堕渶瑕?鍔ㄥ皢瀹炰綋鐨勬暟鎹缃埌 sql 璇彞鐨勫崰浣嶇浣嶇疆 銆?/p>
搴斿涓婅堪闂缁欏嚭鐨勮В鍐?妗堬細
鈶?nbsp;. 浣?鏁版嵁搴撹繛鎺ユ睜鍒濆鍖栬繛鎺ヨ祫婧?銆?/p>
鈶?nbsp;. 灏?nbsp;sql 璇彞鎶藉彇鍒?nbsp;xml 閰嶇疆?浠朵腑 銆?/p>
鈶?nbsp;. 浣?鍙嶅皠绛夊簳灞傛妧鏈紝?鍔ㄥ皢瀹炰綋涓庤〃杩?灞炴€т笌瀛楁鐨?鍔ㄦ槧灏?銆?/p>
Mybatis妗嗘灦浠嬬粛
mybatis 鏄?涓紭绉€鐨勫熀浜?nbsp;java 鐨勬寔涔呭眰妗嗘灦锛屽畠鍐呴儴灏佽浜?nbsp;jdbc 锛屼娇寮€鍙戣€呭彧闇€瑕佸叧娉?nbsp;sql璇彞鏈韩锛?涓嶉渶瑕佽姳璐圭簿?鍘诲鐞嗗姞杞介┍鍔ㄣ€佸垱寤鸿繛鎺ャ€佸垱寤?nbsp;statement 绛夌箒鏉傜殑杩囩▼ 銆?/p>
mybatis 閫氳繃 xml 鎴栨敞瑙g殑?寮忓皢瑕佹墽?鐨勫悇绉?nbsp;statement 閰嶇疆璧锋潵锛屽苟閫氳繃 java 瀵硅薄鍜?nbsp;statement 涓璼ql 鐨勫姩鎬佸弬鏁拌繘?鏄犲皠?鎴愭渶缁堟墽?鐨?nbsp;sql 璇彞 銆?/p>
鏈€鍚?nbsp;mybatis 妗嗘灦鎵? sql 骞跺皢缁撴灉鏄犲皠涓?nbsp;java 瀵硅薄骞惰繑鍥炪€傞噰? ORM鎬濇兂瑙e喅浜嗗疄浣撳拰鏁版嵁搴撴槧灏勭殑闂锛屽 jdbc 杩?浜嗗皝瑁咃紝灞忚斀浜?nbsp;jdbc api 搴曞眰璁块棶缁嗚妭锛屼娇鎴戜滑涓?涓?nbsp;jdbc api 鎵撲氦閬擄紝灏卞彲浠ュ畬鎴愬鏁版嵁搴撶殑鎸佷箙鍖栨搷浣?銆?/p>
MyBatis 瀹?鍦板潃锛?nbsp;https://mybatis.org/mybatis-3/
Mybatis 鏋舵瀯
1). mybatis 閰嶇疆?浠?/p>
SqlMapConfig.xml 锛屾?浠朵綔涓?nbsp;mybatis 鐨勫叏灞€閰嶇疆?浠讹紝閰嶇疆浜?nbsp;mybatis鐨勮繍?鐜绛変俊鎭€俶apper.xml ?浠跺嵆 sql 鏄犲皠?浠讹紝?浠朵腑閰嶇疆浜嗘搷浣滄暟鎹簱鐨?nbsp;sql璇彞銆傛?浠堕渶瑕佸湪SqlMapConfig.xml 涓姞杞姐€?/p>
2). 閫氳繃 mybatis 鐜绛夐厤缃俊鎭瀯閫?nbsp;SqlSessionFactory 鍗充細璇??
3). 鐢变細璇??鍒涘缓 sqlSession 鍗充細璇濓紝鎿嶄綔鏁版嵁搴撻渶瑕侀€氳繃 sqlSession 杩?銆?/p>
4). mybatis 搴曞眰?瀹氫箟浜?nbsp;Executor 鎵?鍣ㄦ帴?鎿嶄綔鏁版嵁搴擄紝 Executor鎺?鏈変袱涓疄鐜帮紝?涓槸鍩烘湰鎵?鍣?nbsp;BaseExecutor 銆?涓槸缂撳瓨鎵?鍣?nbsp;CachingExecutor 銆?/p>
5). MappedStatement 涔熸槸 mybatis ?涓簳灞傚皝瑁呭璞★紝瀹冨寘瑁呬簡 mybatis 閰嶇疆淇℃伅鍙?nbsp;sql鏄犲皠淇℃伅绛夈€俶apper.xml ?浠朵腑?涓?nbsp;sql 瀵瑰簲?涓?nbsp;MappedStatement 瀵硅薄锛?nbsp;sql 鐨?nbsp;id 鍗虫槸 Mapped statement 鐨?nbsp;id 銆?/p>
6). MappedStatement 瀵?nbsp;sql 鎵?杈?鍙傛暟杩?瀹氫箟锛屽寘鎷?nbsp;HashMap 銆佸熀鏈被鍨嬨€?nbsp;pojo 锛?nbsp;Executor閫氳繃Mapped Statement 鍦ㄦ墽? sql 鍓嶅皢杈?鐨?nbsp;java 瀵硅薄鏄犲皠? sql 涓紝杈?鍙傛暟鏄犲皠灏辨槸 jdbc缂栫▼涓 preparedStatement 璁剧疆鍙傛暟銆?/p>
7). MappedStatement 瀵?nbsp;sql 鎵?杈撳嚭缁撴灉杩?瀹氫箟锛屽寘鎷?nbsp;HashMap 銆佸熀鏈被鍨嬨€?nbsp;pojo 锛?nbsp;Executor閫氳繃Mapped Statement 鍦ㄦ墽? sql 鍚庡皢杈撳嚭缁撴灉鏄犲皠? java 瀵硅薄涓紝杈撳嚭缁撴灉鏄犲皠杩囩▼鐩稿綋浜?nbsp;jdbc缂栫▼涓缁撴灉鐨勮В鏋愬鐞嗚繃绋嬨€?/p>
Mybatis蹇€熷叆闂?/h2>
寮€鍙戞楠?/h3>
鈶?nbsp;. 娣诲姞 MyBatis 鐨勫潗鏍?/p>
鈶?nbsp;. 鍑嗗鏁版嵁搴撶幆澧?/p>
鈶?nbsp;. 缂栧啓 User 瀹炰綋绫?/p>
鈶?nbsp;. 缂栧啓鏍??浠?nbsp;SqlMapConfig.xml
鈶?nbsp;. 缂栧啓鏄犲皠?浠?nbsp;UserMapper.xml
鈶?nbsp;. 缂栧啓娴嬭瘯绫?/p>
鐜鎼缓
1 瀵?渚濊禆
-
<!--mybatis鍧愭爣-->
-
<dependency>
-
<groupId>org.mybatis</groupId>
-
<artifactId>mybatis</artifactId>
-
<version>3.4.5</version>
-
</dependency>
-
<!--mysql椹卞姩鍧愭爣-->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
<version>8.0.16</version>
-
</dependency>
-
<!--鍗曞厓娴嬭瘯鍧愭爣-->
-
<dependency>
-
<groupId>junit</groupId>
-
<artifactId>junit</artifactId>
-
<version>4.12</version>
-
</dependency>
-
<!--lombok鍧愭爣-->
-
<dependency>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
<version>1.16.20</version>
-
</dependency>
-
<!--?蹇楀潗鏍?->
-
<dependency>
-
<groupId>log4j</groupId>
-
<artifactId>log4j</artifactId>
-
<version>1.2.17</version>
-
</dependency>
2鍑嗗鏁版嵁搴撶幆澧?/p>
-
create database mybatis;
-
use mybatis;
-
CREATE TABLE `user` (
-
`id` int(11) NOT NULL AUTO_INCREMENT,
-
`username` varchar(32) NOT NULL COMMENT 鈥?鎴峰悕绉扳€?
-
`sex` char(1) DEFAULT NULL COMMENT 鈥樻€у埆鈥?
-
`address` varchar(256) DEFAULT NULL COMMENT 鈥樺湴鍧€鈥?
-
PRIMARY KEY (`id`)
-
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
insert into `user` (`id`, `username`, `sex`, `address`)
-
values(NULL,鈥楾om鈥?鈥樼敺鈥?鈥樺寳浜€?;
-
insert into `user` (`id`, `username`, `sex`, `address`)
-
values(NULL,鈥楯ack鈥?鈥樼敺鈥?鈥樹笂娴封€?;
-
insert into `user` (`id`, `username`, `sex`, `address`)
-
values(NULL,鈥楻ose鈥?鈥?鈥?鈥?瀹夆€?;
-
insert into `user` (`id`, `username`, `sex`, `address`)
-
values(NULL,鈥楶oll鈥?鈥?鈥?鈥樺埄?浜氣€?;
3缂栧啓 User 瀹炰綋
-
@Data
-
public class User {
-
private Integer id;
-
private String username;
-
private String sex;
-
private String address; }
4 缂栧啓 MyBatis鏍稿績鏂囦欢
?浠跺悕 : SqlMapConfig.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>
-
<!--閰嶇疆鏁版嵁搴撹繍?鐜-->
-
<environments default="development">
-
<environment id="development">
-
<transactionManager type="JDBC"/>
-
<dataSource type="POOLED">
-
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
-
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test_wensong"/>
-
<property name="username" value="root"/>
-
<property name="password" value="root"/>
-
</dataSource>
-
</environment>
-
</environments>
-
<!--閰嶇疆鍏宠仈鐨凷QL璇彞鐨勬槧灏?浠?->
-
<mappers>
-
<mapper resource="UserMapper.xml"/>
-
</mappers>
-
</configuration>
5 缂栧啓 UserMapper 鏄犲皠?浠?/p>
?浠跺悕 : UserMapper.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="userMapper">
-
<!--鏌ヨ鎵€鏈?鎴?->
-
<select id="findAll" resultType="com.wensong.pojo.User">
-
select * from user
-
</select>
-
</mapper>
6 鏌ヨ鎵€鏈夌敤鎴?/p>
-
public class UserTest {
-
@Test
-
public void findAllTest() throws IOException {
-
//鍔犺浇鏍?閰嶇疆?浠?SqlMapConfig.xml
-
InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
-
//鏋勯€燬QLSessionFactory瀵硅薄
-
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
-
//鏋勯€燬QLSession瀵硅薄
-
SqlSession sqlSession = sqlSessionFactory.openSession();
-
//鎵?鏌ヨ璇彞
-
List<User> userList = sqlSession.selectList("userMapper.findAll");
-
for (User user : userList) {
-
System.out.println("user = " + user);
-
}
-
//閲婃斁璧勬簮
-
sqlSession.close();
-
}
-
}
鏌ヨ缁撴灉鏄剧ず锛?/h3>
鐭ヨ瘑灏忕粨
-
1). 鎼缓鐜, 瀵?渚濊禆
-
mybatis , mysql鏁版嵁搴撻┍鍔? junit , lombok
-
2). 鍑嗗鏁版嵁搴撶幆澧?/code>
-
3). 缂栧啓瀹炰綋绫籙ser
-
4). 缂栧啓鏍?閰嶇疆?浠?/code>
-
A. mybatis鐨勮繍?鐜 -----> <environments>
-
B. 鍔犺浇鏄犲皠閰嶇疆?浠?-------> <mappers>
-
5). 缂栧啓鏄犲皠閰嶇疆?浠?/code>
-
SQL璇彞
-
6). 缂栧啓娴嬭瘯浠g爜
-
A. 鍔犺浇Mybatis鐨勬牳?閰嶇疆?浠?/code>
-
B. 鏋勯€燬qlSessionFactory
-
C. 鏋勯€燬qlSession
-
D. 璋?SqlSession涓殑selectList鎵?鏌ヨ
-
E. 閲婃斁璧勬簮
1). 鎼缓鐜, 瀵?渚濊禆
mybatis , mysql鏁版嵁搴撻┍鍔? junit , lombok
2). 鍑嗗鏁版嵁搴撶幆澧?/code>
3). 缂栧啓瀹炰綋绫籙ser
4). 缂栧啓鏍?閰嶇疆?浠?/code>
A. mybatis鐨勮繍?鐜 -----> <environments>
B. 鍔犺浇鏄犲皠閰嶇疆?浠?-------> <mappers>
5). 缂栧啓鏄犲皠閰嶇疆?浠?/code>
SQL璇彞
6). 缂栧啓娴嬭瘯浠g爜
A. 鍔犺浇Mybatis鐨勬牳?閰嶇疆?浠?/code>
B. 鏋勯€燬qlSessionFactory
C. 鏋勯€燬qlSession
D. 璋?SqlSession涓殑selectList鎵?鏌ヨ
E. 閲婃斁璧勬簮
以上是关于Mybatis妗嗘灦 鍏ラ棬瀛︿範的主要内容,如果未能解决你的问题,请参考以下文章