MyBatis配置与简单查询

1.配置向导

mybatis-config.xml

(1).<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

(2).

<configuration>

//日志文件
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<environments default="数据库">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="数据库url"/>
<property name="username" value="用户"/>
<property name="password" value="密码" />
</dataSource>

</environment>
</environments>
<mappers>
<mapper resource="映射向导" />
</mappers>
</configuration>

2.日志

前提是导入jar包

log4j.rootLogger=ERROR,stdout
log4j.logger.类.映射向导=DEBUG
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern="%5p[%t]-%m%n"

3.映射向导

(1)

  <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

(2) 命名空间

  <mapper namespace="包.映射向导">

  </mapper>

(3) 插入

<insert id="save" parameterType="类.pojo">
  insert into works values(#{id},#{name},#{sex})
</insert>

(4) 查询

<select id="select1" parameterType="输入类型" resultType="输出类型">
  select * from works
</select>

(5) ResultMaps映射

<resultMap id="workerMap" type="包,pojo">
  <id property="id" column="id"/>
  <result property="name" column="name" />
  <result property="sex" column="sex" />
</resultMap>

(6) 动态sql

 ①. 

<choose>
  <when test="id != null">
    and id=#{id}
  </when>
  <when test="sex != null">
    and sex=#{sex}
  </when>
</choose>

②.

<if test="id != null">
  and id=#{id}
</if>
<if test="sex != null">
  and sex=#{sex}
</if>

③.

<where>
  <if test="id != null">
    and id=#{id}
  </if>
  <if test="sex != null">
    and sex=#{sex}
  </if>
</where>

  

 

上一篇:python 进度条


下一篇:XMind漏洞xss复现(最全payload图文教程)