1. 程式人生 > >mybatis(二)配置入門和lombok元件使用

mybatis(二)配置入門和lombok元件使用

第一步:去setting 配置裡面下載lombok外掛,如下圖所示:

然後重啟。。

第二步:

建立實體類:src/main/java/com/zit/model/Book.java

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;

@Data
public class Book {
    private int id;
    private String name;
    private BigDecimal price;
    private Date btime;
}

按快捷鍵Alt+7;

這時候@Data紅色暫時不用管,

第三步:

去pom.xml檔案中加入:

  <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.2</version>
      <scope>provided</scope>
    </dependency>

等待載入完成,可以看到變化,並且@Data恢復正常,lombok載入成功。

想要多種變化,百度搜搜。。。。

寫一個測試類如下:

src/test/java/Demo.java

import com.zit.model.Book;

import org.junit.Test;

public class Demo {

    @Test
    public void mm() {
        Book bok = new Book();
        System.out.println(bok);

    }
}

結果如下:

Book(id=0, name=null, price=null, btime=null)

*******************************************************************************************************************

接著講配置:

接著在src/main/resources/目錄下建立兩個配置檔案:

檔案1 :db.properties:程式碼如下:

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/dt_game?serverTimezone=PRC&useSSL=false&uerUnicode=true&characterEncoding=utf-8
db.user=root
db.password=

檔案2:conf.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>
	    <!-- 載入db.properties -->
	    <properties resource="db.properties"/>
	    <environments default="development">
		<environment id="development">
		    <transactionManager type="JDBC"/>
		    <dataSource type="POOLED">
			<property name="driver" value="${db.driver}"/>
			<property name="url" value="${db.url}"/>
			<property name="username" value="${db.username}"/>
			<property name="password" value="${db.password}"/>
		    </dataSource>
		</environment>
	    </environments>
	    <mappers>
		<!-- 載入對映配置檔案 -->
		<mapper resource="com/fz/mapper/BookMapper.xml"/>
	    </mappers>
	</configuration>

配置檔案整理好之後,開始寫檔案:

檔案1:src/main/java/com/zit/model:  Book.java

package com.zit.model;

import lombok.Data;

import java.math.BigDecimal;
import java.util.Date;
@Data
public class Book {
    private int id;
    private String name;
    private BigDecimal price;
    private Date btime;
}

檔案2:src/main/java/com/zit/mapper:  BookMapper.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="book">
    <insert id="add">
        insert into book values(null,#{name},#{price},#{btime})
    </insert>

    <select id="queryall" resultType="com.zit.model.Book">
        select * from book
    </select>

   <select id="queryById" parameterType="int" resultType="com.zit.model.Book">
        select * from book where id=#{id}
    </select>

    <delete id="del">
        delete from book where id=#{id}
    </delete>

    <update id="update">
        update book set name=#{name},price=#{price},btime=#{btime} where id=#{id}
    </update>

</mapper>

檔案2:src/test/java/Demo.java

import com.zit.model.Book;
import com.zit.model.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;


import java.io.IOException;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class Demo {
    @Test
    public void ttt() {
        try {
            Reader reader = Resources.getResourceAsReader("conf.xml");
            SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(reader);
            SqlSession session = sf.openSession();
            Book bk = new Book();
            bk.setBtime(new Date());
            bk.setName("《web專案》");
            bk.setPrice(new BigDecimal(39));
            //session.insert("book.add", bk);


         /*   List<Book> list=session.selectList("book.queryall");
            System.out.println(list);
            for(Book b:list){
                System.out.println(b);
            }*/
        /* session.delete("book.del",6);*/
          /*  System.out.println(session.selectOne("book.queryById",1));*/
            Book bb=session.selectOne("book.queryById",1);
            bb.setPrice(new BigDecimal(20));
            session.update("book.update",bb);

            //session.delete();
            session.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    @Test
    public void mm() {
        Book bok = new Book();
        System.out.println(bok);

        System.out.println(new Student());
    }
}

完成。。。。。