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鐨勫畨瑁呮暀绋嬶紝浠ュ厤浣犵湡鏄釜灏忕櫧鍟婏紒锛侊紒

鎶€鏈浘鐗? src=2.1 pom.xml

pom 閲岄潰灏辨槸瀛︿範mybatis涓€浜涗緷璧栦簡鍏朵腑鐨?junit鐩墠娌$敤鍒帮紝浣嗘槸涓嬭妭璇惧彲鑳藉氨鐢ㄥ埌鍟︼紝鑻嶅ぉ楗惰繃璋佸晩锛侊紒锛?/p>

鎶€鏈浘鐗? src=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閫昏緫鍜紝鍝ュ摜杩欒竟鍋氫簡鏌ヨ鍏ㄨ〃鐨勬搷浣滐紱

鎶€鏈浘鐗? src=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

鎶€鏈浘鐗? src=Mybatis妗嗘灦 鍏ラ棬瀛︿範

webpack鍏ラ棬-涓汉瀛︿範璧勬簮鏀堕泦

Java鍏ラ棬(鍩虹绡囦竴)

鍊嶅鍏ラ棬

JavaPoet鍏ラ棬

cmake鍏ラ棬