JAVA程式設計114——Mybtais配置檔案詳解
阿新 • • 發佈:2018-12-01
參考網站:
一、目錄結構
二、程式碼詳解
1、maven核心配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
<modelVersion>4.0.0</modelVersion>
<groupId>com.mollen.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</ groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
< version>5.1.38</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<!-- 可以解決idea不能編譯src目錄下的xml檔案 -->
<build>
<!--載入src目錄下的靜態檔案-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<!--jdk編譯版本設定-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、mybatis核心配置檔案:SqlMapConfig.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">
<!--mybatis環境配置-->
<configuration>
<!--載入資料來源檔案-->
<properties resource="db.properties"></properties>
<!--配置別名-->
<typeAliases>
<package name="com.mollen.dao"/>
<package name="com.mollen.bean"/>
</typeAliases>
<!--配置資料來源-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</dataSource>
</environment>
</environments>
<!--配置對映/-->
<mappers>
<!--單個對映配置-->
<!--<mapper resource="com/mollen/dao/UserDao.xml"/>-->
<!--批量對映配置-->
<package name="com.mollen.dao"/>
</mappers>
</configuration>
3、配置資料來源檔案
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis_test
jdbc.username=root
jdbc.password=root
4、對映檔案配置:UserDao.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="com.mollen.dao.UserDao">
<!--配置 查詢結果的列名和實體類的屬性名的對應關係/暫不考慮多表對映-->
<resultMap id="userMap" type="user">
<!--主鍵欄位的對應-->
<id property="id" column="id"></id>
<!-- 非主鍵欄位的對應-->
<result property="username" column="username"></result>
<result property="birthday" column="birthday"></result>
<result property="sex" column="sex"></result>
<result property="address" column="address"></result>
</resultMap>
<!--查詢所有-->
<select id="findAll" resultType="user">
SELECT * FROM user;
</select>
<!--根據id查詢使用者-->
<select id="findUserById" resultMap="userMap">
SELECT * FROM user WHERE id in
<foreach collection="array" open="(" close=")" separator="," item="uid">
#{uid}
</foreach>
</select>
<!--根據使用者模糊查詢-->
<select id="findByName" resultMap="userMap">
select * from user where username like #{name}
</select>
<!--根據條件模糊查詢-->
<select id="findByCondition" resultMap="userMap">
SELECT * FROM user WHERE 1=1
<if test="username != null">
AND username like #{username}
</if>
<if test="sex != null">
AND sex like #{sex}
</if>
</select>
<!--新增使用者-->
<insert id="saveOne">
INSERT INTO
user
VALUES
(
null,
#{username},
#{birthday},
#{sex},
#{address}
)
</insert>
<!--刪除使用者/根據傳入引數型別自動匹配sql引數值-->
<delete id="deleteOne" >
DELETE FROM
user
WHERE
id = #{id}
</delete>
<!--修改使用者-->
<update id="updateUser">
UPDATE
user
SET
username = #{username},
birthday = #{birthday},
sex = #{sex},
address = #{address}
WHERE
id = #{id}
</update>
</mapper>
5、Dao介面類:UserDao.java
package com.mollen.dao;
import com.mollen.bean.User;
import java.util.List;
/**
* @ClassName: UserDao
* @Auther: Mollen
* @CreateTime: 2018-10-27 20:19:07
* @Description:
*/
public interface UserDao {
/**
* 1.查詢所有
* @return
*/
public List<User> findAll();
/**
* 2.根據id查詢使用者(動態執行引數)
* @return
*/
public List<User> findUserById(Integer...ids);
/**
* 3.根據使用者模糊查詢
*/
public List<User> findByName(String username);
/**
* 4.根據條件模糊查詢
* @param user
* @return
*/
public List<User> findByCondition(User user);
/**
* 5.新增一條資料
* @param user
*/
public void saveOne(User user);
/**
* 6.刪除一條資料
* @param user
*/
public void deleteOne(User user);
/**
* 7.修改使用者
*/
public void updateUser(User user);
}
6、實體類:User.java
package com.mollen.bean;
import java.util.Date;
/**
* @ClassName: User
* @Auther: Mollen
* @CreateTime: 2018-10-29 10:11:48
* @Description:
*/
public class User {
private int id;
private String username;
private Date birthday;
private String sex;
private String address;
//getter/setter/toString/...
}