- 创建Maven项目
- 根据数据库,创建实体类,遵循下划线转驼峰命名
- 创建对实体类进行dao操作的 映射接口 ,并在映射接口中定义方法。
- 创建映射文件 xxxMapper.Xml ,根据映射接口定义其中的mapper内容
- 在MyBatis的配置文件中,配置引用我们的映射文件,添加log4j的配置文件.properties文件,并修改路径。
- 测试类 获取SqlSessionFactory
Reader read = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(read);
SqlSession sqlSession = sqlSessionFactory.openSession();
SysUserMapper userMapper = sqlSession.getMapper(SysUserMapper.class);
第一:maven项目创建成功,效果如下
第二:修改pom.xml文件,我们需要配置Maven项目运行的基本环境,比如我们要进行单元测试,要连接数据库,都需要有jar包进行支持。在该文件中添加如下内容:
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <build> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>l.6</source> <target>l.6</target> </configuration> </plugin> </plugins> </build> <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.1</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> </dependency> <dependency> <groupId>org.slf4j</groupId> |