目录结构如下:
在pom.xml中配置需要的jar包
org.mybatis mybatis 3.3.0 mysql mysql-connector-java 5.1.29 junit junit 4.11 test log4j log4j 1.2.17 org.slf4j slf4j-api 1.7.12 org.slf4j slf4j-log4j12 1.7.12
接下来新建config.properties配置文件将JDBC连接所需参数配置进去
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
username=root
password=123456
建一个model,Person.java
public class Person {private int id;private String userName ;private int age ;private String mobilePhone ;public Person(){}public Person(int id,String userName, int age, String mobilePhone) {this.id = id;this.userName = userName;this.age = age;this.mobilePhone = mobilePhone;}public String getUserName() {return userName;}public int getId() {return id;}public void setId(int id) {this.id = id;}public void setUserName(String userName) {this.userName = userName;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getMobilePhone() {return mobilePhone;}public void setMobilePhone(String mobilePhone) {this.mobilePhone = mobilePhone;}@Overridepublic String toString() {return "Person{" +"userName='" + userName + ''' +", age=" + age +", mobilePhone='" + mobilePhone + ''' +'}';}
}
配置mybatis-config.xml
配置日志打印log4j.properties
log4j.rootLogger=debug, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%5p - %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=firestorm.loglog4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%nlog4j.logger.com.codefutures=DEBUG
配置mapper包下的Person.xml
INSERT INTO PERSON(ID,USERNAME,AGE,MOBILEPHONE)VALUES (#{id},#{userName},#{age},#{mobilePhone}) UPDATE PERSON SET USERNAME=#{userName},AGE=#{age},MOBILEPHONE=#{mobilePhone} WHERE ID=#{id}
写一个工具类来获取SqlSessionFactory和SqlSession
public class MybatisUtil {private final static SqlSessionFactory sqlSessionFactory;static {String resource="mybatis-config.xml";Reader reader =null;try {reader = Resources.getResourceAsReader(resource);} catch (IOException e) {e.printStackTrace();}sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);}/*** 获取SqlSessionFactory* @return SqlSessionFactory*/public static SqlSessionFactory getSqlSessionFactory(){return sqlSessionFactory;}/*** 获取SqlSession* @return SqlSession*/public static SqlSession getSqlSession(){return sqlSessionFactory.openSession();}/*** 关闭SqlSession*/public static void closeSession(SqlSession sqlSession){if (sqlSession!=null)sqlSession.close();}}
最后测试
public class UserTest {SqlSession sqlSession ;@Testpublic void insertPerson(){sqlSession = MybatisUtil.getSqlSession();int id = 10000;String userName = "test";int age = 18;String mobilePhone = "18000000000";Person person = new Person();person.setId(id);person.setAge(age);person.setUserName(userName);person.setMobilePhone(mobilePhone);try{sqlSession.insert("insertPerson",person);sqlSession.commit();}catch (Exception e){e.printStackTrace();}finally {MybatisUtil.closeSession(sqlSession);}}@Testpublic void queryById(){sqlSession = MybatisUtil.getSqlSession();int id = 1;try{Person person = sqlSession.selectOne("queryById",id);sqlSession.commit();System.out.println(person.getUserName());}catch (Exception e){e.printStackTrace();}finally {MybatisUtil.closeSession(sqlSession);}}
}
最后测试,成功插入到数据库中