1、核心配置文件 sqlMapConfig.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <properties resource="jdbc.properties" /> 7 <!-- 别名 包以其子包下所有类 头字母大小都行 --> 8 <typeAliases> 9 <!-- <typeAlias type="cn.lijun.mybatis.pojo.User" alias="User"/> --> 10 <package name="com.zlh.mybatis.pojo" /> 11 </typeAliases> 12 <!-- 和spring整合后 environments配置将废除 --> 13 <environments default="development"> 14 <environment id="development"> 15 <!-- 使用jdbc事务管理 --> 16 <transactionManager type="JDBC" /> 17 <!-- 数据库连接池 --> 18 <dataSource type="POOLED"> 19 <property name="driver" value="${jdbc.driver}" /> 20 <property name="url" value="${jdbc.url}" /> 21 <property name="username" value="${jdbc.user}" /> 22 <property name="password" value="${jdbc.password}" /> 23 </dataSource> 24 </environment> 25 </environments> 26 <!-- 映射器 --> 27 <mappers> 28 <mapper resource="sqlmap/User.xml" /> 29 <mapper resource="com/zlh/mybatis/mapper/UserMapper.xml"/> 30 <mapper resource="com/zlh/test/test.xml"/> 31 </mappers> 32 </configuration>
2、sql语句 User.xml
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 <mapper namespace="user"> 6 <select id="findUserById" parameterType="Integer" resultType="com.zlh.mybatis.pojo.User" > 7 SELECT * FROM user WHERE id = #{id} 8 </select> 9 <select id="findUserByUserName" parameterType="String" resultType="com.zlh.mybatis.pojo.User" > 10 SELECT * FROM user WHERE username LIKE "%"#{username}"%" 11 </select> 12 <select id="findAllUser" parameterType="Integer" resultType="com.zlh.mybatis.pojo.User" > 13 SELECT * FROM user 14 </select> 15 <insert id="insertUser" parameterType="com.zlh.mybatis.pojo.User"> 16 INSERT INTO user (username,birthday,sex,address) VALUES (#{username},#{birthday},#{sex},#{address}) 17 </insert> 18 <update id="updateUser" parameterType="com.zlh.mybatis.pojo.User"> 19 UPDATE user SET username = #{username},sex=#{sex},birthday=#{birthday},address=#{address} WHERE id = #{id} 20 </update> 21 <delete id="deleteUserById" parameterType="Integer"> 22 DELETE FROM user WHERE id = #{id} 23 </delete> 24 </mapper>
3、日志文件 log4j.properties
1 # Global logging configuration 2 log4j.rootLogger=DEBUG, stdout 3 # Console output... 4 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 5 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 6 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
4、数据库文件 jdbc.properties
1 jdbc.driver=com.mysql.jdbc.Driver 2 jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8 3 jdbc.user=root 4 jdbc.password=root
5、测试类
1 package com.zlh.mybatis.junit; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 import org.junit.Test; 11 12 import com.zlh.mybatis.pojo.User; 13 14 public class MybatisFirstTest { 15 16 @Test 17 public void testMybatis() throws IOException { 18 // 1加载核心配置文件 19 String resource = "sqlMapConfig.xml"; 20 InputStream in = Resources.getResourceAsStream(resource); 21 // 2创建sqlSessionFactory 22 SqlSessionFactory s = new SqlSessionFactoryBuilder().build(in); 23 // 3创建sqlSession 24 SqlSession sqlSession = s.openSession(); 25 // 4执行sql语句 26 User u = sqlSession.selectOne("user.findUserById", 10); 27 System.out.println(u); 28 } 29 }