序文
今日は、MyBatisの統合スプリングブートについて学びましょう。通常、MyBatisを統合するには2つの方法があります。 1つは注釈に基づいており、もう1つはXML構成に基づいています。今日は、最初に注釈ベースのMyBatis統合について学びましょう。以下ではあまり言わない、詳細な紹介を見てみましょう
MyBatisであるため、MyBatisに関連している必要があり、MySQLを使用しているため、MySQL関連も導入する必要があります。
<! - https://mvnrepository.com/artifact/org.mybatis.spring.boot.boot.boot.boot/mybatis-spring-boot-starter-> <依存関係> groupid> org.mybatis.spring.boot.boot </groupid> <artifactid> mybatis-spring-spring-boot-startid </dependency> <! - https://mvnrepository.com/artifact/mysql/mysql-connector-java-> <seplency> mysql> mysql> mysql </groupid> <artifactid> mysql-connector-java </artifactid> <バージョン> 8.0.11 </バージョン>
ここでは、ユーザーモデルが作成されます。これは、データベーステーブルとの比較に便利です。ここでは、MyBatisという名前のデータベースがMySQLで作成され、ユーザーテーブルが作成されます。同時に、列挙クラスのユーザーセクセンが作成されます。
テーブル「user」( `id` int(11)not null auto_increment、` name` varchar(20)default null、 `age` int(11)デフォルトnull、` sex` varchar(20)デフォルトnull、プライマリキー( `id`))エンジン= innodb auto_increment = 9 default charset = utf8;
パッケージcom.example.model; import java.io.serializable; public classユーザーはSerializable {@override public string toString(){// todo auto-feenated methood stub Returt "user [id =" + name + "、age =" + age + "]; } public int getId(){return id; } public void setid(int id){id = id; } public string getname(){return name; } public void setName(string name){name = name; } public int getage(){return age; } public void Setage(int age){age = age; } private int id;プライベート文字列名;プライベートインクエイジ;プライベートユーザーセクセンセックス; public usersexenum getsex(){return sex; } public void setSex(usersexenum sex){sex = sex; }}パッケージcom.example.model; public Enum usersexenum {man、woman}ここでは、モデルをデータベースを操作するSQLと比較する必要があります。どの比較を使用する必要がありますか?次に、マッパーを作成する必要があります。ここには、追加、削除、変更、検索があります。
パッケージcom.example.mapper; import java.util.list; import org.apache.ibatis.annotations.delete; import org.apache.ibatis.annotations.insert; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Reg.apach.annotations. org.apache.ibatis.annotations.update;インポートcom.example.model。 * ;; public interface usermapper {@serect( "select * from user")@results({result( "sex"、column = "sex"、javatype = usersexenum.class)、 @rusult(colum = ""、column = ""、column = " @SeLect( "select * from user where id =#{id}")@results({ @result(property = "sex"、column = "sex"、javatype = usersexenum.class)、 @result(property = "name"、column = "name")})ユーザーgetone(int id); @insert( "Inserting Into user(name、age、sex)values(#{name}、#{age}、#{sex})")void insert(user user); @update( "Update users set name =#{username}、age =#{age} where id =#{id}")void update(user user); @delete( "ユーザーからの削除id =#{id}")void delete(int id);}マッパーは上記で構成されているので、システムはどのようにマッパーが配置されているかをどのように知ることができますか?したがって、Annotation @mapperscanがあります。
パッケージcom.example.demo; import org.mybatis.spring.annotation.mapperscan; Import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootapplication; static void main(string [] args){springApplication.run(demoapplication.class、args); }}ここでは、usercontrollerが作成されます。1つはすべてのユーザーを表示することであり、もう1つは新しいユーザーを追加して、すべてのユーザーを表示することです。
package com.example.demo;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.requestmethod; Import com.example.mapper.usermapper; Import com.example.model.user; Import com.example.model.usersexenum; @RequestMapping(value = "/alluser.do"、method = requestmethod.get)public string getAllusers(モデルモデル){list <user> users = usermapper.getall(); model.addattribute( "users"、users); 「userlist」を返します。 } @RequestMapping(value = "/insert.do"、method = requestmethod.get)public string adduser(model model){user user = new user(); user.setname( "cuiyw"); user.setage(27); user.setsex(usersexenum.man); usermapper.insert(user);リスト<ユーザー> users = usermapper.getall(); model.addattribute( "users"、users); 「userlist」を返します。 }}マッパーとモデルも上に設定されています。データベースと対話するには、データベースアドレスとその他の情報を構成する必要があります。ここで実行されるときにエラーが報告されました。ネストされた例外はjava.sql.sqlexceptionです:サーバータイムゾーン値 'Öð¹ú±ê׼걼理は認識されていないか、1つ以上のタイムゾーンを表します。タイムゾーンサポートを利用する場合は、より具体的なタイムゾーン値を使用するように、サーバーまたはJDBCドライバー(Servertimezone Configurationプロパティを介して)のいずれかを構成する必要があります。次のタイムゾーンはmysqlで設定されます:Global time_zone = '+8:00'を設定します。
spring.mvc.view.prefix =/view/spring.mvc.view.suffix = .jspmybatis.type-aliase-package = com.example.modelspring.datasource.driverclassname = com.mysql.cj.jdbc.driverspring.datasource.url = JDBC:mysql:// localhost:3306/mybatisspring.datasource.username = rootspring.datasource.password = 123456
7.表示するページを作成します
ここでは、以前のブログに従ってJSPを使用してデータを表示します。
<%@ page Language = "Java" contentType = "text/html; charset = utf-8" pageencoding = "utf-8"%> <%@ taglib prefix = "c" uri = "http://java.sun.com/jsp/jsp/jstl/core"トランジション// en "" http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <tr> <th> name </th> <th> age </th> <th> gender </th> </tr> <c:foreach items = "$ {users}" var = "item"> <tr> <td> $ {item.name} </td> <td> $ {item.age}} </td> <td> <td> <td> </td> </c:foreach> </table> </body> </html>ここで、最初にhttp:// localhost:8080/user/alluser.doブラウザーで、ユーザーリストを表示してから、http:// localhost:8080/user/insert.doを入力できます。リストに追加のデータラインが表示されます。
MyBatisの注釈ベースの統合を使用する方が簡単で便利ですが、利点と短所の両方があります。複数のテーブルが接続されるのがそれほど便利ではない場合があり、XMLベースの構成を使用する方が良いかもしれません。
さて、上記はこの記事のコンテンツ全体です。この記事の内容には、すべての人の研究や仕事に特定の参照値があることを願っています。ご質問がある場合は、メッセージを残してコミュニケーションをとることができます。 wulin.comへのご支援ありがとうございます。