mybatis 學習二 MyBatis簡介與配置MyBatis+Spring+MySql

來源:互聯網
上載者:User

標籤:actor   stat   url   ace   into   final   指定   ltm   config   

1.2.2建立MySql資料庫

  在C:\Program Files\MySQL\MySQL Server 5.7\bin下面:  

  首先串連MySQL:        mysql  -u root -p

/* 建立資料庫 */  CREATE DATABASE STUDENT_MANAGER;  USE STUDENT_MANAGER;    /***** 建立student表 *****/  CREATE TABLE STUDENT_TBL  (     STUDENT_ID         VARCHAR(255) PRIMARY KEY,     STUDENT_NAME       VARCHAR(10) NOT NULL,     STUDENT_SEX        VARCHAR(10),     STUDENT_BIRTHDAY   DATE,     CLASS_ID           VARCHAR(255)  );    /*插入學生資料*/  INSERT INTO STUDENT_TBL (STUDENT_ID,                           STUDENT_NAME,                           STUDENT_SEX,                           STUDENT_BIRTHDAY,                           CLASS_ID)    VALUES   (123456,              ‘某某某‘,              ‘女‘,              ‘1980-08-01‘,              121546              )  

建立串連MySql使用的設定檔mysql.properties。

Mysql.properties代碼  
jdbc.driverClassName=com.mysql.jdbc.Driver  jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-8  
1.2.3搭建MyBatis環境1.2.3.1建立實體類: StudentEntity

 

package com.deppon.test04.entity;import java.io.Serializable;import java.util.Date;public class StudentEntity implements Serializable {            private static final long serialVersionUID = 3096154202413606831L;      private Date studentBirthday;      private String studentID;      private String studentName;      private String studentSex;        public Date getStudentBirthday() {          return studentBirthday;      }        public String getStudentID() {          return studentID;      }        public String getStudentName() {          return studentName;      }        public String getStudentSex() {          return studentSex;      }        public void setStudentBirthday(Date studentBirthday) {          this.studentBirthday = studentBirthday;      }        public void setStudentID(String studentID) {          this.studentID = studentID;      }        public void setStudentName(String studentName) {          this.studentName = studentName;      }        public void setStudentSex(String studentSex) {          this.studentSex = studentSex;      }  }  
1.2.3.2建立資料提供者

Student類對應的dao介面:StudentMapper。

public interface StudentMapper {            public StudentEntity getStudent(String studentID);            public StudentEntity getStudentAndClass(String studentID);            public List<StudentEntity> getStudentAll();            public void insertStudent(StudentEntity entity);            public void deleteStudent(StudentEntity entity);            public void updateStudent(StudentEntity entity);  }  
1.2.3.3建立SQL映射語句檔案

 Student類的sql語句檔案StudentMapper.xml
resultMap標籤:表欄位與屬性的映射。
Select標籤:查詢sql。

<?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="com.deppon.test04.dao.StudentMapper">        <resultMap type="StudentEntity" id="studentResultMap">          <id property="studentID" column="STUDENT_ID"/>          <result property="studentName" column="STUDENT_NAME"/>          <result property="studentSex" column="STUDENT_SEX"/>          <result property="studentBirthday" column="STUDENT_BIRTHDAY"/>      </resultMap>            <!-- 查詢學生,根據id -->      <select id="getStudent" parameterType="String" resultType="com.deppon.test04.entity.StudentEntity" resultMap="studentResultMap">          <![CDATA[             SELECT * from STUDENT_TBL ST                 WHERE ST.STUDENT_ID = #{studentID}          ]]>       </select>            <!-- 查詢學生列表 -->      <select id="getStudentAll"  resultType="com.deppon.test04.entity.StudentEntity" resultMap="studentResultMap">          <![CDATA[             SELECT * from STUDENT_TBL         ]]>       </select>        </mapper>  
1.2.3.4建立MyBatis的mapper設定檔
在src/main/resource中建立MyBatis設定檔:mybatis-config.xml。
typeAliases標籤:給類起一個別名。com.manager.data.model.StudentEntity類,可以使用StudentEntity代替。
Mappers標籤:載入MyBatis中實體類的SQL映射語句檔案。
<?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>             <typeAlias type="com.deppon.test04.entity.StudentEntity" alias="StudentEntity"/>      </typeAliases>            <mappers>          <mapper resource="src/main/resources/StudentMapper.xml" />      </mappers>    </configuration>  

 

1.2.3.5修改Spring 的設定檔

主要是添加SqlSession的製作工廠類的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定設定檔位置和dataSource。
和資料提供者對應的實現bean。通過MapperFactoryBean建立出來。需要執行介面類全稱和SqlSession工廠bean的引用

mybatis 學習二 MyBatis簡介與配置MyBatis+Spring+MySql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.