mybatis第一个例子
第一步创建sqlMapConfig.xml
这个文件相当于hibernate里的hibernate.cfg.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">
<!--数据库环境配置:如果需要和spring整合就不需要配置了完全交给spring-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/personal_database?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--引入映射文件 注意包和包之间用/-->
<mapper resource="configure/mapper/PersonTestMapper.xml"/>
</mappers>
</configuration>
第二步建立model类
public class Person {
private Integer id;
private String name;
private Integer gender;
private String address;
private Date birthday;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", gender=" + gender +
", address='" + address + '\'' +
", birthday=" + birthday +
'}';
}
}
第三步创建PersonTestMapper.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" >
<!--
namespace:映射文件的命名空间,规范是映射文件的包的全路径
-->
<mapper namespace="configure.mapper.PersonTestMapper">
<!--id:sql的唯一标识
parameterType:传递给sql的参数的数据类型
resultType:返回数据结果集合的类型
#{id}:用于接收参数的语法,{}中的内容,如果接受一个参数内容任意
#{}使用预编译方式生成sql,优点是防止sql注入
-->
<select id="selectPersonById" parameterType="java.lang.Integer" resultType="model.Person">
select *from person_test p where p.id=#{id}
</select>
</mapper>
第四步测试类
public class PersonTest {
SqlSessionFactory sf;
@Before
public void setUP() throws IOException {
InputStream in= Resources.getResourceAsStream("configure/sqlMapConfig.xml");
sf=new SqlSessionFactoryBuilder().build(in);
}
@Test
public void test() {
SqlSession session = sf.openSession();
try {
Person person = session.selectOne("configure.mapper.PersonMapper.selectPersonById", 1);
System.out.println(person);
}finally {
session.close();
}
}
}
2、mybatis控制台无日志输出解决
2.1在pom.xml中添加依赖
<!--Log4j日志输出功能-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--日志控制-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>compile</scope>
</dependency>
2.2在resources中加入log4j.properties
log4j.rootLogger=DEBUG, Console
#Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
2.3sqlMapConfig.xml添加setting
原因:可能你导入了多个日志工具jar,导致mybatis不知道使用哪种日志。修改你的mybatis配置,添加setting,指定日志类型为log4j。
解决方法:
<configuration>
<!-- 设置使用的日志类型 -->
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
</configuration>