标签:config 2.4 eve font doctype 基于 耦合 命名 log4j
1 为什么要使用Mybatis?
1.1 使用JDBC、DBUtils、JDBC Template等工具进行开发
1.2 使用Hibernate来进行开发
1.3 Mybatis框架:半自动、轻量级框架
2 Mybatis简介
2.1 Mybatis简介
2.2 Mybatis历史
2.3 为什么要使用Mybatis?
2.4 下载地址
3 Mybatis入门--HelloWorld
3.1 创建数据库及表
create database mybatis; user database mybatis; create table employee( id int(11) primary key auto_increment, last_name varchar(255), gender char(1), email varchar(255) );
3.2 创建POJO
package cn.demo1; /** * 描述:POJO */ public class Employee { private Integer id; private String lastName; private String gender; private String email; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
3.3 导入jar包及log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.err log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file mylog.log ### log4j.appender.file=org.apache.log4j.FileAppender log4j.appender.file.File=c\:mylog.log log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ### log4j.rootLogger=off, stdout
3.4 导入Mybatis的核心配置文件
<?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> <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/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource=""/> </mappers> </configuration>
3.5 创建映射文件
<?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="default"> <!-- namespace:命名空间 id:唯一标识 resultType:返回值类型 --> <select id="findById" parameterType="int" resultType="cn.demo1.Employee"> select * from employee where id = #{id} </select> </mapper>
3.6 在核心配置文件中配置映射文件
<?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> <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/mybatis"/> <property name="username" value="root"/> <property name="password" value="root"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/demo1/EmployeeMapper.xml"/> </mappers> </configuration>
3.7 导入MySQL的驱动
3.8 建立测试类
package cn.test; import cn.demo1.Employee; 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 org.junit.Test; import java.io.IOException; import java.io.InputStream; /** * * 描述: */ public class MybatisTest { @Test public void demo1() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); Employee employee = session.selectOne("default.findById", 1); System.out.print(employee); session.close(); } }
怎么解决?数据库中的字段和POJO中的属性值不一样呢,通过别名,即通过将SQL的别名设置为POJO中对应的属性值。
<?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="default"> <!-- namespace:命名空间 id:唯一标识 resultType:返回值类型 --> <select id="findById" parameterType="int" resultType="cn.demo1.Employee"> select id,last_name lastName,email,gender from employee where id = #{id} </select> </mapper>
4 Mybatis入门--HelloWorld的接口编程
4.1 新建一个接口
package cn.demo1; public interface IEmployeeDAO { public Employee findById(Integer id); }
4.2 修改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 namespace="cn.demo1.IEmployeeDAO"> <!-- namespace:命名空间 指定为接口的全类名 id:唯一标识 和接口中的方法名相同 resultType:返回值类型 --> <select id="findById" parameterType="int" resultType="cn.demo1.Employee"> select id,last_name lastName,email,gender from employee where id = #{id} </select> </mapper>
4.3 测试
package cn.test; import cn.demo1.Employee; import cn.demo1.IEmployeeDAO; 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 org.junit.Test; import java.io.IOException; import java.io.InputStream; /** * * 描述: */ public class MybatisTest { @Test public void demo1() throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); IEmployeeDAO dao = session.getMapper(IEmployeeDAO.class); Employee employee = dao.findById(1); System.out.print(employee); session.close(); } }
5 SqlSession
标签:config 2.4 eve font doctype 基于 耦合 命名 log4j
原文地址:http://www.cnblogs.com/xuweiweiailixing/p/6884060.html