Mybatis入门
1.环境搭建
1.新建maven项目,在pom.xml中添加依赖(普通项目直接导包也是一样的)
<packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> </dependencies>
2.写配置文件SqlMapConfig.xml,叫啥名都可以
mappers中的配置,是实体类所对应的的配置文件,xml方式就写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"> <!--mybatis的主配置文件--> <configuration> <!--配置环境--> <environments default="mysql"> <!--配置Mysql的环境,id和default必须相同--> <environment id="mysql"> <!--配置事务类型--> <transactionManager type="JDBC"></transactionManager> <!--配置数据源(也叫连接池)--> <dataSource type="POOLED"> <!--配置连接数据库的4个基本信息--> <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis_study?characterEncoding=UTF-8"/> <property name="username" value="root"/> <property name="password" value="2013.71123"/> </dataSource> </environment> </environments> <!--配置映射文件目录:xml方式--> <mappers> <mapper resource="com/itheima/dao/IUserMapper.xml"></mapper> </mappers> <!--配置映射文件目录:注解方式 全限定类名 使用注解方式就不需要IUserMapper.xml配置文件了 --> <!--<mappers>--> <!--<mapper class="com.itheima.dao.IUserDao"></mapper>--> <!--</mappers>--> </configuration>
3.编写实体类和相对应的mapper映射文件
实体类,get和set方法就不截图了
映射文件
<?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是哪个接口的映射文件 --> <mapper namespace="com.itheima.dao.IUserDao"> <!--id为接口中的方法名--> <select id="findAll" resultType="com.itheima.domain.User"> select * from user; </select> </mapper>
4.测试findAll方法
@Test public void TestFindAll() throws IOException { //1.读取配置文件 InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml"); //2.创建SqlSessionFactory SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(in); //3.使用工厂创建SqlSession对象 SqlSession session = sessionFactory.openSession(); //4.使用SqlSession对象创建Dao接口的代理对象 IUserDao userDao = session.getMapper(IUserDao.class); //5.使用代理对象执行方法 List<User> all = userDao.findAll(); for (User user:all) { System.out.println(user); } //6.释放资源 in.close(); session.close(); }
另外:log4j的配置文件log4j.properties
### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out 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' ### # error warn info debug trace log4j.rootLogger= debug,stdout