myBatis也是一个持久型框架,相较于hibernate来说,算是轻量级的。
1.配置mybatis环境
相关jar下载地址:mybatis+mysalJAR包
2.新建一个java project工程
2.1配置log4j.properties文件,用于日志输出
log4j.rootCategory=DEBUG, 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
log4j
2.2配置MybatisConfig.xml,用于配置数据库连接
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mynatis.org//DTD Config 3.0 EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 别名 --> <typeAliases> <typeAlias alias="User" type="com.juin.entity.User" /> <typeAlias alias="Person" type="com.juin.entity.Person" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"> </transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_test"/> <property name="username" value="juin"/> <property name="password" value="123654"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/juin/map/juinUser.xml"/> <mapper resource="com/juin/map/person.xml"/> <mapper class="com.juin.map.IUser"/> </mappers> </configuration>
MyBatisConfig.xml
2.3配置juinUser.xml,前面忘记说了,mybatis实现了代码与数据库连接语句的分离。
<?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="domain.blog.mappers.AuthorMapper"> <select id="findById" parameterType="int" resultType="com.juin.entity.User"> select * from user where id=#{id} </select> <insert id="insertUser" parameterType="User" statementType="PREPARED" keyProperty = "id" useGeneratedKeys = "true"> insert into user (username,psw) values (#{username},#{psw}) </insert> <update id="updateUser" parameterType="User"> UPDATE User SET username=#{username}, psw=#{psw} where id = #{id} </update> <select id="loginSelect" parameterType="hashmap" resultType="User"> select * from user where username = #{username} and psw = #{psw} </select> <select id="loginSelect2" parameterType="User" resultType="User"> select * from user where username = #{username} and psw = #{psw} </select> <select id="selectList" resultType="User"> select * from user </select> <resultMap id="UserMap" type="User"> <id property="id" column="id"/> <result property="username" column="username"/> <result property="psw" column="psw"/> </resultMap> <select id="selectUsers" resultMap="UserMap"> select * from user </select> </mapper>
juinUser.xml
2.4创建User实体类
package com.juin.entity; public class User { private int id; private String username; private String psw; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPsw() { return psw; } public void setPsw(String psw) { this.psw = psw; } }
User.java
2.5创建测试类
package com.juin.test; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.juin.entity.User; public class test1 { public static void main(String[] args) { String resource = "com/juin/map/MybatisConfig.xml"; Reader reader = null; SqlSession session = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } SqlSessionFactory sqlMapper = new SqlSessionFactoryBuilder().build(reader); session = sqlMapper.openSession(); User temp = session.selectOne("findById", 2); System.out.println(temp.getUsername()); session.close(); } }
test