筆記之_Java整理MyBaties單用

mybatis中#和$綁定參數:
#{}將傳入的數據都當成一個字符串,會對自動傳入的數據加一個雙引號。如:order by #{id},若是傳入的值是111,那麼解析成sql時的值爲order by 「111」, 若是傳入的值是id,則解析成的sql爲order by 「id」。
    ${}將傳入的數據直接顯示生成在sql中。如:order by
${id},若是傳入的值是111,那麼解析成sql時的值爲order by 111, 若是傳入的值是id,則解析成的sql爲order by id。
    #方式可以很大程度防止sql注入。
    $方式沒法防止Sql注入。
    $方式通常用於傳入數據庫對象,例如傳入表名.
    通常能用#的就別用$.
mybaties-config.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>
    <!-- 讀取數據加配置文件   jdbc.properties -->
    <properties resource="jdbc.properties"/>
     <settings>
       <!-- 在控制檯打印sql語句 -->
        <setting name="logImpl" value="LOG4J"/>
     </settings>
     <!-- 對實體類能夠少寫完整路徑 -->
     <typeAliases>
        <package name="com.xxxxx.entity"/>
     </typeAliases>
    <!-- 配置環境變量,裏面能夠配置N個數據庫 -->
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />
            </dataSource>
        </environment>
    </environments>
    <!-- 讓mybaits來管理你所定義的全部自定義的文件  -->
    <mappers> 
        <!-- 搜索 java接口 -->
         <package name="com.xxxxx.dao"/> 
    </mappers>
</configuration>










Mapper.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.xxxxxxx.dao.MajorMapper">
        <insert id="insert" parameterType="com.xxxxxxx.entity.Major"
          useGeneratedKeys="true" keyProperty="majorId"
         flushCache="true">
           insert into major (majorName) values (#{majorName})
      </insert>
       <!-- 更新 -->
      <update id="update" parameterType="com.xxxxxxx.entity.Major">
          update major set majorName=#{majorName}
          where majorId=#{majorId}
      </update>
      <!-- 簡單的查詢  -->
      <select id="findById" parameterType="int" resultType="com.xxxxxxx.entity.Major">
          select * from major where majorId=#{majorId}
      </select>
       <!-- 根據條件查詢  -->
       <select id="find" resultType="com.xxxxxxx.entity.Major">
          select * from major           
          <where>
               <if test="majorName!=null">
                  and  majorName like #{majorName}
               </if>
          </where>
       </select>
</mapper>













MyBatiesUtil.java寫法:
public class MyBtaisUtil {
    private static SqlSessionFactory  sessionFactory=null;
    static{
        InputStream in=MyBtaisUtil.class.getResourceAsStream("/mybatis-config.xml");
        sessionFactory=new SqlSessionFactoryBuilder().build(in);        
    }
    public static SqlSession openSession(){
        return  sessionFactory.openSession();
    }
    public static SqlSession openSession(boolean autoCommit){
        return sessionFactory.openSession(autoCommit);
    }
}
使用Mybaties:
public class MajorServiceImpl implements MajorService {
    @Override
    public boolean insert(Major obj) {
        SqlSession session = MyBtaisUtil.openSession();
        MajorMapper mapper = session.getMapper(MajorMapper.class);
        try {
            mapper.insert(obj);
            session.commit();
            return true;
        } catch (Exception e) {
            session.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }
        return false;
    }
}
相關文章
相關標籤/搜索