将之前的学习代码整理一下发一下
导包之后在src下创建
log4j.properties
SqlMapConfig.xml
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">
<configuration>
<!-- 和spring整合后 environments配置将废除-->
<!-- 这个包名下的类可以直接使用了 不用写路径 -->
<typeAliases>
<package name="tt" />
</typeAliases>
<environments default="database1">
<environment id="database1">
<!-- 使用jdbc事务管理-->
<transactionManager type="JDBC" />
<!-- 数据库连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/licong?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<mappers>
<package name="test" />
</mappers>
</configuration>
log4j:
### \u8BBE\u7F6ELogger\u8F93\u51FA\u7EA7\u522B\u548C\u8F93\u51FA\u76EE\u7684\u5730 ###
log4j.rootLogger=debug,stdout,logfile
### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
### \u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u6587\u4EF6\uFF1Ajbit.log ###
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=jbit.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %F %p %m%n
###\u663E\u793ASQL\u8BED\u53E5\u90E8\u5206
log4j.logger.com.ibatis=DEBUG
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=DEBUG
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
在包tt(忽略命名)下 创建 user类
package tt;
public class user {
private int id;
private String name;
@Override
public String toString() {
return "user [id=" + id + ", name=" + name + "]";
}
public user(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public user(){super();}
}
创建mybatiesutils.java实例去加载 xml文件
package tt;
import java.io.IOException;
import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private static SqlSessionFactory sqlSessionFactory = null;
static {
InputStream in;
try {
in = Resources.getResourceAsStream("SqlMapConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
private MyBatisUtil() {
};
public static SqlSession getSqlSession() {
return sqlSessionFactory.openSession();
}
}
根据xml中 mappers 创建包test
然后创建接口 inteface
package test;
import tt.user;
public interface inteface {
user findUserById(user user);
user selectbytitle(String title);
}
再然后创建inteface.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="test.inteface">
<sql id="column">
'title',
'deng'
</sql>
<select id="findUserById" parameterType="user" resultType="tt.user">
select * from count
where 1=1
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="name!=null and name!=''">
and name like '%${name}%'
</if>
</select>
<select id="selectbytitle" parameterType="String" resultType="tt.user">
select * from count where name=#{title}
</select>
</mapper>
最后测试一下 写一个测试类Tests
public class Tests {
@Test
public void testFindUserById() {
//获取SqlSession
SqlSession session = MyBatisUtil.getSqlSession();
//解析class文件获取接口方法
inteface blogMapper = session.getMapper(inteface.class);
user uu=new user();
//赋值
uu.setName("李四");
user blog = blogMapper.findUserById(uu);
user bb=blogMapper.selectbytitle("李四");
System.out.println(blog);
System.out.println(bb);
session.close();
}
}
运行 结束