Maven的pom.xml 坐标配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Mybatis_maven</groupId> <artifactId>day01_mbatis</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> <!--数据库 mysql 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <!--junit 测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> </plugins> </build> </project>
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> <typeAliases> <package name=""/> </typeAliases> <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/test1"/> <property name="username" value="xxx"/> <property name="password" value="xxx"/> </dataSource> </environment> </environments> <mappers> <!-- 注意映射文件的路径与所对应的接口包路径一致, 或者干脆放在同一个包中--> <mapper resource="com/itheima/dao/IUserDao.xml"/> </mappers> </configuration>
mapper文件 (命名最好与所对应的接口一致:如:IUserDao 那么mapper可以叫 IUserDaoMapper.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="com.itheima.dao.IUserDao"> <!-- id的值必须时idao接口中的对应方法名 --> <select id="findAll" resultType="com.itheima.domain.User"> select * from user </select> </mapper>
测试类:(重点注意测试类的调用方式)
package com.itheima.test; import com.itheima.dao.IUserDao; import com.itheima.domain.User; 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 java.io.InputStream; import java.util.List; public class MybatisTest { public static void main(String[] args) throws Exception { InputStream in = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); SqlSessionFactory s = builder.build(in); SqlSession sqlSession = s.openSession(); //通过SqlSession创建Dao接口的代理对象 IUserDao userdao = sqlSession.getMapper(IUserDao.class); List<User> users = userdao.findAll(); for(User u :users){ System.out.println(u); 结果:User{id=8, username='heheh', birthday=Sat Aug 24 00:00:00 CST 2019, sex='女', address='huana'} } sqlSession.close(); } }
注解的方式:
在上面的实例中直接把对应的Mapper文件删除掉
原来xml配置的接口 删除mapperxml配置后 在接口的方法上上对应的注解:
原来的mybatis中的配置 一个是 resource中取
修改后的mybaits配置 一个是class下取