mybaits
导包
asm-3.3.1.jar
cglib-2.2.2.jar
javassist-3.17.1-GA.jar
mybatis-3.2.1.jar
mysql-connector-java-5.1.26-bin.jar
mybatis-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>
<!--引入数据库链接信息-->
<properties resource="jdbc.properties"/>
<!--
typeAliases:配置别名
注:别名不区别大小写
-->
<typeAliases>
<!-- type:类型 alias:别名-->
<!--<typeAlias type="cn.itsource.domain.Product" alias="Product" />-->
<!--为这个包下面的所有类取别名(就是类名)-->
<package name="com.wal.mybatis.domain"/>
</typeAliases>
<!--选中其中一个环境-->
<environments default="development">
<environment id="development">
<!--
事务管理 type:JDBC(支持事务)/MANAGED(什么都不做)
-->
<transactionManager type="JDBC"></transactionManager>
<!-- 数据源, 连接池 type(POOLED):MyBatis自带的连接池 -->
<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>
<mappers>
<!--引入写sql的xml-->
<mapper resource="com/wal/mybatis/dao/EmployeeMapper.xml"/>
</mappers>
</configuration>
EmployeeMapper.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:根(每个xml都得有,不用管它的含义)
namespace:命名空间(随便取个名称)
-->
<mapper namespace="com.wal.mybatis.dao.EmployeeMapper">
<!--
select:代表这是一个查询语句
id:代表这个查询语句的唯一命名
以后你要找到这条SQL: namespace+id
parameterType:参数类型
long -> Long _long -> long
resultType:返回的每一条结果的类型
注:返回类型的全限定名
-->
<select id="getOne" parameterType="long" resultType="employee">
select * from employee where id=#{id}
</select>
<select id="getAll" resultType="employee">
select * from employee
</select>
<insert id="save" parameterType="employee">
insert into employee (username,password,email,headImage,age,department_id)
values (#{username},#{password},#{email},#{headImage},#{age},#{department_id})
</insert>
<delete id="delete" parameterType="long">
delete from employee where id=#{id}
</delete>
<update id="update" parameterType="employee">
update employee set
username=#{username},
password=#{password},
email=#{email},
headImage=#{headImage},
age=#{age},
department_id=#{department_id}
where id=#{id}
</update>
<!-- delete from product where id in ( ? , ? ) 批量删除:
collection="list":传入的list,相当于map的key,通过list得到传入的整个集合的值;
index="index" :每次循环的索引
item="id" :每次循环的这个值
open="(" :以什么开始
separator=",":分隔符
close=")":以什么结束
-->
<delete id="deleteBatch" parameterType="list">
delete from employee where id in
<foreach collection="list" item="id" open="(" separator="," close=")">
#{id}
</foreach>
</delete>
<!-- 批量插入:
insert into product(productName,salePrice,costPrice,cutoff,supplier,brand,dir_id)
values (?,?,?,?,?,?,?) , (?,?,?,?,?,?,?)
parameterType:传入参数是:java.util.List的类型的;
对传入的list进行foreach循环:取出值填入插入的values中:
collection="list":这个不能修改,表示传入的一个list类似于一个key是list,得到传入的list集合
index="index":每一次遍历的序号
item="item":每次遍历的这个对象别名,可以修改
separator=",":每遍历一次后的分隔符
-->
<insert id="insertBatch" parameterType="list">
insert into employee (username,password,email,headImage,age,department_id)
values
<foreach collection="list" item="employee" separator=",">
(#{employee.username},#{employee.password},#{employee.email},#{employee.headImage},#{employee.age},#{employee.department_id})
</foreach>
</insert>
<!--动态修改-->
<update id="updateBatch" parameterType="employee">
update employee
<set>
<if test="username!=null">
username=#{username},
</if>
<if test="password!=null">
password=#{password},
</if>
<if test="email!=null">
email=#{email},
</if>
<if test="headImage!=null">
headImage=#{headImage},
</if>
<if test="age!=null">
age=#{age},
</if>
<if test="department_id!=null">
department_id=#{department_id}
</if>
</set>
where id=#{id}
</update>
</mapper>
log4j
导包
commons-logging-1.1.1.jar
log4j-1.2.17.jar
slf4j-api-1.7.2.jar
slf4j-log4j12-1.7.2.jar//先有的log4j后有的slf4j规范,所以需要这个适配包
log4j.properties
# ERROR错误的日志 WARN:警告 INFO:普通信息 DEBUG:调试日志 TRACE:日志
log4j.rootLogger=ERROR, stdout
#log4j.rootLogger=NONE
#把左边包名改成你自己的包名
log4j.logger.com.wal.mybatis=TRACE
# 日志打印到控制台中
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# 日志打印的一种格式(可以灵活地指定布局模式)
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# 日志打印的格式是什么样子的 %d:日期 %p:优先级 %c:类的全名 %m:输出的结果 %n:换行
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n