Maven mybatis的框架搭建
Posted Litter1996
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Maven mybatis的框架搭建相关的知识,希望对你有一定的参考价值。
小编用的是 IDEA 2017.2 maven搭建环境
1.pom.xml
1 <!--mybatis需要的依赖--> 2 <dependency> 3 <groupId>org.mybatis</groupId> 4 <artifactId>mybatis</artifactId> 5 <version>3.4.1</version> 6 </dependency> 7 8 <!--mysql需要的依赖--> 9 <dependency> 10 <groupId>mysql</groupId> 11 <artifactId>mysql-connector-java</artifactId> 12 <version>5.1.38</version> 13 </dependency> 14 15 <!-- 日志文件管理包 --> 16 <dependency> 17 <groupId>log4j</groupId> 18 <artifactId>log4j</artifactId> 19 <version>1.2.17</version> 20 </dependency> 21 <dependency> 22 <groupId>org.slf4j</groupId> 23 <artifactId>slf4j-api</artifactId> 24 <version>1.7.12</version> 25 </dependency> 26 <dependency> 27 <groupId>org.slf4j</groupId> 28 <artifactId>slf4j-log4j12</artifactId> 29 <version>1.7.12</version> 30 </dependency>
2.Bean
Employee
1 package com.mozi.bean; 2 3 public class Employee { 4 5 private Integer id; 6 private String lastName; 7 private String email; 8 private String gender; 9 10 public Employee(){} 11 12 @Override 13 public String toString() { 14 return "Employee{" + 15 "id=" + id + 16 ", lastName=‘" + lastName + ‘\‘‘ + 17 ", email=‘" + email + ‘\‘‘ + 18 ", gender=‘" + gender + ‘\‘‘ + 19 ‘}‘; 20 } 21 22 public Employee(Integer id, String lastName, String email, String gender) { 23 this.id = id; 24 this.lastName = lastName; 25 this.email = email; 26 this.gender = gender; 27 } 28 29 public Integer getId() { 30 31 return id; 32 } 33 34 public void setId(Integer id) { 35 this.id = id; 36 } 37 38 public String getLastName() { 39 return lastName; 40 } 41 42 public void setLastName(String lastName) { 43 this.lastName = lastName; 44 } 45 46 public String getEmail() { 47 return email; 48 } 49 50 public void setEmail(String email) { 51 this.email = email; 52 } 53 54 public String getGender() { 55 return gender; 56 } 57 58 public void setGender(String gender) { 59 this.gender = gender; 60 } 61 }
3.Bean的映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper 3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> 5 6 <!-- 7 namespace:名称空间 8 id:唯一标识 9 resultType:返回值类型 10 #{id} 从传递过来的参数中取出id值 11 12 --> 13 <mapper namespace="com.mozi.bean.EmployeeMapper"> 14 <select id="selectEmp" resultType="com.mozi.bean.Employee"> 15 select id,last_name lastName,email,gender from table_mybatis where id = #{id} 16 </select> 17 </mapper>
4.全局映射文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="com.mysql.jdbc.Driver"/> 11 <property name="url" value="jdbc:mysql://localhost:3306/mybtis"/> 12 <property name="username" value="root"/> 13 <property name="password" value=""/> 14 </dataSource> 15 </environment> 16 </environments> 17 <mappers> 18 <mapper resource="EmployeeMapper.xml"/> 19 </mappers> 20 </configuration>
5.测试类
1 package com.mozi.bean; 2 3 import org.apache.ibatis.io.ResolverUtil; 4 import org.apache.ibatis.io.Resources; 5 import org.apache.ibatis.session.SqlSession; 6 import org.apache.ibatis.session.SqlSessionFactory; 7 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 8 9 import java.io.IOException; 10 import java.io.InputStream; 11 12 public class mybatisTest { 13 public static void main(String args[]) throws IOException { 14 mybatisTest mybatisTest = new mybatisTest(); 15 mybatisTest.Test(); 16 } 17 /** 18 * 1、根据xml配置文件(全局配置文件)创建一个SqlSessionFactory对象 19 * 2、sql映射文件,配置了每一个sql,以及sql的封装规则等 20 * 3、将sql映射文件注册在全局配置文件中 21 * (1)、根据全局配置文件得到sqlSessionFactory 22 * (2)、使用sqlFactory工厂,获取sqlSession对象使用他来执行增删改查 23 * 一个sqlSession就是代表和数据库的一次会话,用完关闭 24 * (3)、使用sql的唯一标志来告诉Mybatis执行哪个sql、sql保存在映射文件中 25 * @throws IOException 26 */ 27 public void Test() throws IOException { 28 String resource = "mybatis.xml"; 29 InputStream inputStream = Resources.getResourceAsStream(resource); 30 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 31 32 //2.获取sqlSession实例,能直接执行已经映射的sql语句 33 SqlSession openSession = sqlSessionFactory.openSession(); 34 try { 35 Employee employee = openSession.selectOne("com.mozi.bean.EmployeeMapper.selectEmp", 1); 36 System.out.println(employee); 37 }finally { 38 openSession.close(); 39 } 40 } 41 }
6.log4j
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 3 4 <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> 5 6 <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> 7 <param name="Encoding" value="UTF-8" /> 8 <layout class="org.apache.log4j.PatternLayout"> 9 <param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" /> 10 </layout> 11 </appender> 12 <logger name="java.sql"> 13 <level value="debug" /> 14 </logger> 15 <logger name="org.apache.ibatis"> 16 <level value="info" /> 17 </logger> 18 <root> 19 <level value="debug" /> 20 <appender-ref ref="STDOUT" /> 21 </root> 22 </log4j:configuration>
以上是关于Maven mybatis的框架搭建的主要内容,如果未能解决你的问题,请参考以下文章
使用intellij idea搭建MAVEN+springmvc+mybatis框架
基于Maven的Springboot+Mybatis+Druid+Swagger2+mybatis-generator框架环境搭建
手把手Maven搭建SpringMVC+Spring+MyBatis框架(超级详细版)