mybatis鍏ラ棬
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis鍏ラ棬相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/pom' title='pom'>pom 灏忕櫧 mybatis console sys void 鏂囦欢 鏍囩 鎸佷箙
涓€ 鍓嶈█
MyBatis 鏄竴娆句紭绉€鐨勬寔涔呭眰妗嗘灦锛屽畠鏀寔瀹氬埗鍖?SQL銆佸瓨鍌ㄨ繃绋嬩互鍙婇珮绾ф槧灏勩€侻yBatis 閬垮厤浜嗗嚑涔庢墍鏈夌殑 JDBC 浠g爜鍜屾墜鍔ㄨ缃弬鏁颁互鍙婅幏鍙栫粨鏋滈泦銆侻yBatis 鍙互浣跨敤绠€鍗曠殑 XML 鎴栨敞瑙f潵閰嶇疆鍜屾槧灏勫師鐢熺被鍨嬨€佹帴鍙e拰 Java 鐨?POJO锛圥lain Old Java Objects锛屾櫘閫氳€佸紡 Java 瀵硅薄锛変负鏁版嵁搴撲腑鐨勮褰曘€?/p>
浜?mybatis鍏ラ棬
灏忕櫧浣犲ソ鍟婏紝鍦ㄨ繖閲屼綘瑕佽嚜宸卞垱寤簃aven宸ョ▼锛屽鏋滀笉鎳傜殑閰嶇疆瀹夎鍙互鐪嬫垜tool绯诲垪鐨?a href="https://blog.csdn.net/youku1327/article/details/101866709">idea閰嶇疆maven锛?br /> 濡傛灉鏁版嵁搴撴病鏈夊杩囷紝鍙互鐪嬫垜sql绯诲垪鐨勬暀绋嬶紱鍦ㄤ笅鏂?properties閲岄潰杩橀殣钘忎簡涓€涓猰ysql鐨勫畨瑁呮暀绋嬶紝浠ュ厤浣犵湡鏄釜灏忕櫧鍟婏紒锛侊紒
2.1 pom.xml
pom 閲岄潰灏辨槸瀛︿範mybatis涓€浜涗緷璧栦簡鍏朵腑鐨?junit鐩墠娌$敤鍒帮紝浣嗘槸涓嬭妭璇惧彲鑳藉氨鐢ㄥ埌鍟︼紝鑻嶅ぉ楗惰繃璋佸晩锛侊紒锛?/p>
mysql support --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>6.0.5</version> </dependency> </dependencies>
2.2 log4j.properties
閰嶇疆log4j.properties鏄负浜嗘洿濂芥樉绀烘棩蹇椾俊鎭紝鑳藉鎵撳嵃鍑簊ql锛涙垜浠湪 resource鐩綍涓嬫柊寤轰竴涓?log4j.properties锛屽唴瀹瑰涓嬶紝璁板緱鏇挎崲mapper鍖呯殑鏄犲皠璺緞鍝熷皬鐧斤紱瑕佷笉鐒舵棩蹇楁墦鍗颁笉鍑烘潵鏈変綘鍝殑锛屾姳鐫€鍝ュ摜鐨勫崥瀹㈠拰鍏紬鍙烽兘娌¤蒋鐢紱
### 璁剧疆###
log4j.rootLogger = info,stdout
### 杈撳嚭淇℃伅鍒版帶鍒舵姮 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
## 杩欒竟瑕佹浛鎹㈡垚浣犵殑mapper鍖呮槧灏勮矾寰勫摕
log4j.logger.com.zszxz.mybatis.mapper = debug
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
# mysql瀹夎https://zhuanlan.zhihu.com/p/37152572
2.4 瀛︾敓瀹炰綋
瀛︾敓瀹炰綋鏄负浜嗘煡璇ql璇彞灏嗚繑鍥炵殑缁撴灉鏄犲皠鍒板疄浣撻噷闈㈠摕锛?/p>
/**
1. @Author lsc
2. @Description <p> </p>
3. @Date 2019/12/1 20:07
*/
public class Student {
// id
private String id;
// 瀛︾敓濮撳悕
private String name;
// 瀛︾敓缂栧彿
private String number;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}
2.5 mapper
涓嬮潰鐨勬瘡鍙ヨ瘽閮藉牚绉扮粡鍏革紝浣犲鏋滄槸寮傛暀寰掞紝瑕佸儚瀛︿範鍦g粡涓€鏍疯檾璇氬寰呭畠锛?/strong>
- StudentMapper.xml 鏄?涓€涓獂ml 鏂囦欢 閲岄潰涓昏鏄?mybatis 鐨?DTD 锛岃繕鏈夋垜浠啓鐨剆ql閫昏緫锛?/li>
- 娉ㄦ剰 杩欒竟鏈変釜
namespace
灞炴€ф槸璺熸垜浠?StudentMapper 鎺ュ彛瀵瑰簲鏄犲皠璺緞锛?/li> - 涓€涓?mapper.xml 瀵瑰簲涓€涓?mapper 鎺ュ彛 鍒悶娣峰挴锛岄€氬父浠栦滑鐨勫悕瀛楅兘涓€涓摕锛屽畼鏂逛範鎯互mapper缁撳熬鍝燂紱
- StudentMapper.xml 鍒涘缓鐨勪綅缃?鏄湪 resource鐩綍鐨刴apper瀛愮洰褰曚腑锛屽鏋滀綘鐪熸槸灏忕櫧灏卞埆鑷畾涔夊摕锛屼細鍑洪敊鐨勶紱
- StudentMapper 閲岄潰鏈変釜鏂规硶
getStudent
鏄窡<select>
鏍囩閲岄潰鐨刬d 瀵瑰簲锛岃〃绀?鏌ヨ鏂规硶鐨勫敮涓€鏍囪瘑锛?/li> resultType
閲岄潰灏辨槸 瀛︾敓瀹炰綋鐨勬槧灏勮矾寰勶紝涓嶈濉被鍚嶏紝涓囦竴鎶ラ敊浜嗭紝浣犲彲鎵句笉鍒版垜甯綘妫€鏌ヤ簡锛?/li><select>
鏍囩閲岄潰鐨勫唴瀹瑰氨鏄垜浠殑sql閫昏緫鍜紝鍝ュ摜杩欒竟鍋氫簡鏌ヨ鍏ㄨ〃鐨勬搷浣滐紱
2.6 mybatis-config.xml - 瀹冨氨鏄竴涓厤缃?/h2>
mybatis-config.xml 鏄垜浠?閰嶇疆mybatis 鐨勫唴瀹归兘鍦ㄩ噷闈紝灏忕櫧鍦ㄨ繖閲屼笉鐢ㄧ煡閬撳お澶氾紝灏忚閲岄潰鏈夊彞璇濆緢缁忓吀鍟婏紝鐭ラ亾鐨勮秺澶氭鐨勮秺蹇晩锛屼綘瑕佸鏉ㄤ慨涔堬紝铏界劧鎵嶅崕妯孩锛岃繕涓嶆槸琚浌鎿嶅挃鍤撲簡锛侊紒锛侊紒浣犺鐭ラ亾鐨勬垜閮藉啓閲岄潰锛涜繖涓枃浠剁殑浣嶇疆 鏄?strong>鏀惧湪 resource鐩綍涓?/strong>锛?/p>
<?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">
<!-- mybaits閰嶇疆 -->
<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://192.168.0.106:3306/mybatis"/>
<!-- 璐﹀彿-->
<property name="username" value="root"/>
<!--瀵嗙爜 -->
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!-- 寮曞叆鑷畾涔塵apper.xml -->
<mappers>
<mapper resource="mapper/StudentMapper.xml"/>
</mappers>
</configuration>
2.7 Mybatis蹇窇
寤虹珛涓€涓富绫伙紝璁╂垜浠厤缃殑mybatis璺戣捣鏉ユ妸锛侊紒锛?/p>
- SqlSessionFactoryBuilder 鏄缓閫犵被锛屼负浜嗚幏寰?SqlSessionFactory锛屼綘鍙互绉板懠瀹冧负 sql 浼氳瘽锛坰ession锛?宸ュ巶
- SqlSessionFactory 宸ュ巶閲岄潰鏈夊ソ澶歋qlSession锛屾瘡涓嚎绋嬮兘搴旇鏈変竴涓?SqlSession(sql 浼氳瘽)锛屼娇鐢?code>openSession() f鏂规硶鑾峰緱SqlSession锛?/li>
- 鎵ц瀹宻ql鎿嶄綔鐨勬柟娉曞埆蹇樿鍏抽棴sqlSession 鍝燂紝瑕佷笉鐒朵互鍚庣鐩樺穿浜嗗埆鎵炬垜锛侊紒锛?/li>
public static void main(String[] args) throws IOException {
// 璧勬簮璺緞
String resource = "mybatis-config.xml";
// 閰嶇疆mybatis鑾峰緱杈撳叆娴? InputStream inputStream = Resources.getResourceAsStream(resource);
// 鍒涘缓 SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//浠?SqlSessionFactory 涓幏鍙?SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 浣跨敤 SqlSession 鏌ヨ
List<Student> getStudent = sqlSession.selectList("getStudent");
for (Student student : getStudent){
System.out.println("瀛︾敓濮撳悕锛?quot;+student.getName()+" 瀛︾敓缂栧彿锛?quot;+student.getNumber());
}
// 鍏抽棴 SqlSession
sqlSession.close();
}
涓?婧愮爜婧愮爜鍦ㄥ摢閲?/h1>
灏忕櫧鍐欒瘯璇曡嚜宸辨暡鍟婏紝涓嶈浜嗗啀鐪嬬湅鍝ュ摜鐨勬簮鐮侊紒锛侊紒锛侊紒锛侊紒
youku1327鐨刧ithub
Mybatis妗嗘灦 鍏ラ棬瀛︿範