您好,登錄后才能下訂單哦!
mybasits配置文件書寫
1.configer文件配置
<?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>
<!-- 使用Mybaits的日志控制 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="default"> <!-- 定義所有的數據庫鏈接,并指定使用哪一個數據源 -->
<environment id="default"><!-- 定義數據源名稱 -->
<transactionManager type="JDBC" /><!-- 事務的提交類型 -->
<dataSource type="POOLED"> <!-- 定義一個數據源 ,連接方式為數據庫連接池方式 -->
<property name="driver" value="com.mysql.cj.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test4012?characterEncoding=utf8&serverTimezone=GMT"/>
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<!-- 加載數據庫鏈接的時候,加載的sql映射 -->
<mappers>
<mapper resource="com/pojo/usermapper-4012.xml" />
<mappers>
</configuration>
2.mapper文件配置
(1)resultType是返回值類型
(2)pramatetertype是傳入的參數
<?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.service.IUserDao4012"><!--接口的地址-->
<!-- 表示聲明一個數據庫的操作包名 -->
<!-- 聲明一個數據庫的操作方法 -->
<!-- 查詢全部內容的方法 -->
<select id="SelectAll" resultType="com.pojo.User4012">
SELECT * FROM table4012
limit #{offset},#{pagesize}
</select>
<!-- 插入操作-->
<insert id="InsertUser" parameterType="com.pojo.User4012">
insert into table4012(id,username,number,mybasits,android,javaee) values (#{id},#{username},#{number},#{mybasits},#{android},#{javaee})
</insert>
<delete id="DeleteUser" parameterType="com.pojo.User4012">
delete from table4012 where id=#{**}
</delete>
<update id="UpdateUser" parameterType="com.pojo.User4012">
update table4012 set username=#{username},number =#{number},mybasits=#{mybasits},android=#{android},javaee=#{javaee} where id=#{id}
</update>
<select id="findcount" parameterType="com.pojo.PageWays">
SELECT count () from table4012
</select>
<select id="findpage" parameterType="com.pojo.PageWays">
SELECT * from table4012
</select>
</mapper>
3.util類配置
package com.SelfStudy.util;
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 java.io.InputStream;
public class Util {
private static SqlSessionFactory build;//定義一個對象
static {
String s="mybatis.config.xml";//加載config文件
InputStream inputStream=null;//輸入輸出流
try {
inputStream= Resources.getResourceAsStream(s);
build=new SqlSessionFactoryBuilder().build(inputStream);
}
catch (Exception e){
e.printStackTrace();
}
finally {
try {
if ( inputStream !=null){
inputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
public static SqlSession getSession(){
return build.openSession();
}
}
4.log4j.properties文件配置
log4j.appender.console =org.apache.log4j.ConsoleAppender
log4j.appender.console.Target =System.out
log4j.appender.console.layout =org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =[%-5p][%d{yyyy-MM-dd HH:mm:ss}] %c %L %m%n
log4j.rootLogger =debug,console
##log4j.logger.com.mapper.StudentMapper=debug,console
四.動態sql
1.if set標簽的使用
Select from table
if(test="name!=null and name!=''"){
#{id}
}
如果是數據庫添加語句在每一句后面添加,
if(test="name!=null and name!=''"){
#{id},
}
2.where標簽的使用
Select from table
<where>
if(test="name!=null and name!=''"){
name=#{name}
}
如果是數據庫添加語句在每一句后面添加,
if(test="id!=null and id!=''"){
id=#{id}
}
</where>
3.choose when標簽的使用
<where>
<choose>
<when test="name!=null and name!=''">
and name=#{name}
</when>
<when></when>
</choose>
</where>
id
=peopledatil.id
tid
=t.id
id
=i.writer_id
LEFT JOIN works o ON o.book_id
=i.works_id
</select>免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。