Mybatis 学习之一:Mybatis+Mysql环境搭建(使用jdbc)
环境:eclipse + maven+ mybatis + mysql
第一步:创建项目 (略)
第二步: 配置maven
(1) :
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <mybatis.version>3.2.0</mybatis.version> <slf4j.version>1.7.7</slf4j.version> <logback.version>1.1.2</logback.version> <mysql.version>5.1.34</mysql.version> </properties>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
<exclusions>『mybatis 使用的是log4j,如果不引入log4j会报错,下文详述』
<!-- 使用了jcl104-over-slf4j转到slf4j -->
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
第三步:model层新建类orderBean.java
右键点击刚创建的包,New一个【class】接口文件,命名为:orderBean.java
package com.mobilelotu.core.model; import java.math.BigDecimal; import java.util.Date; public class OrderBean { private Long id; private String orderCode; …… …… }
第四步:dao层新建接口OrderDao.java
右键点击刚创建的包,New一个【Interface】接口文件,命名为:OrderDao.java
写一个insert的接口:
import com.mobilelotu.core.model.OrderBean; public interface OrderDao { public void insertOrder(OrderBean bean) throws Exception; }
第五步:配置mybatis:
新建文件命名为:mybatis.cfg.xml,配置mybatis.cfg.xml文件。
(1)mybatis.cfg.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <typeAliases> <typeAlias type="com.mobilelotu.core.model.OrderBean" alias="OrderBean" /> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="jdbc"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://127.0.0.1:3306/lottrade" /> <property name="username" value="**" /> <property name="password" value="**" /> </dataSource> </environment> </environments> <mappers> <mapper resource="sqlmap/order_Mapper.xml" />『设置正确的路径:相对路径』 </mappers> </configuration>
namespace是对应Dao层接口类的地址;id对应Dao层接口类中具体的方法名;parameterType是指插入的数据从UserBean中 获取;当Bean中数据类型是字符串,则数据库中数据对应字段的类型只要不是日期类型,则都可以直接使用jdbcType=VARCHAR来转换后直接插 入即可。
(2)order_Mapper.xml 配置如下:
新建文件命名为:order_Mapper.xml ,配置mybatis.cfg.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.mobilelotu.core.dao.OrderDao"> <insert id="insertOrder" parameterType="OrderBean"> insert into user_order ( orderCode ) values ( #{orderCode,jdbcType=VARCHAR}, ) </insert> </mapper>
下面开始测试
第六步:新建test.java
package com.mobilelotu.core; import java.io.IOException; import java.io.Reader; import org.apache.ibatis.io.Resources; import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.mobilelotu.core.dao.OrderDao; import com.mobilelotu.core.model.OrderBean; public class test { private static Log log = LogFactory.getLog(test.class); private static OrderDao orderdao; private static SqlSession sqlSession; private static SqlSessionFactory sessionFactoty; static { String resource = "mybatis.cfg.xml"; Reader reader = null; try { reader = Resources.getResourceAsReader(resource); } catch (IOException ioe) { ioe.printStackTrace(); } sessionFactoty = new SqlSessionFactoryBuilder().build(reader); } public static void main(String[] args) { sqlSession = sessionFactoty.openSession(); orderdao = sqlSession.getMapper(OrderDao.class); OrderBean bean = new OrderBean(); bean.setOrderCode("yangfan"); try { orderdao.insertOrder(bean); sqlSession.commit();『缺少commit,会看到日志中intert有insert语句,但是数据库中没有记录』 } catch (Exception e) { log.error("", e); }finally{ sqlSession.close(); } } }
第七步:项目结构如下图:
第九步:日志输出图下
16:49:18.213 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Priority
因为没有用log4j呀,加上log4j包把
如果项目总体的日志管理用的不是log4j,maven的引入mybatise时加速exclusions 配置吧(见第二步:maven配置)