赞
踩
1.配置数据库加载等的各种资源
<?xml v
rsion="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 和spring整合后 environments配置将废除-->
<environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理-->
<transactionManager type="JDBC" /> <!-- 数据库连接池--> <dataSource type="POOLED">
<proprty name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="1234" /> </dataSource>
<!--必记词汇 environment:上面是配置环境 mapper:映射,dataSource:数据源--> </environment> </environments>
<mappers> <!--这里用于配置映射 --> <mapper resource="org.mybatis.example.BlogMapper.xml"/>
<!--另一种方式,不直接配置完整路径, 直接写UserMapper.xml --> </mappers> </configuration>
<!--SqlMapConfig.xml中主要配置两大块内容,上面是环境,下面是映射 -->
第二步:在resource中编写properties.xml文件UserMapper.xml
<?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> <!-- 和spring整合后 environments配置将废除--> <environments default="development"> <environment id="development"> <!-- 使用jdbc事务管理--> <transactionManager type="JDBC" /> <!-- 数据库连接池--> <dataSource type="POOLED"> <property name="driver" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8" /> <property name="username" value="root" /> <property name="password" value="1234" /> </dataSource> <!--必记词汇 environment:上面是配置环境 mapper:映射,dataSource:数据源--> </environment> </environments> <mappers> <!--这里用于配置映射 --> <mapper resource="org.mybatis.example.BlogMapper.xml"/> <!--另一种方式,不直接配置完整路径, 直接写UserMapper.xml "org.mybatis.example.BlogMapper.xml":这条代码是来自于命名空间后面的内容 --> </mappers> </configuration> <!--SqlMapConfig.xml中主要配置两大块内容,上面是环境,下面是映射 -->
第三步写个持久类:缺乏技术含量,略过
第四步:log4j。properties,主要是用于读取编写后的日志,一般人看不懂
# Global logging configuration log4j.rootLogger=DEBUG, stdout # Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
第五部测试
package com.iotek.mybatis; //测试类这里要加注解 import com.iotek.pojo.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 org.junit.Test; import javax.annotation.Resource; import java.io.IOException; import java.io.InputStream; public class test { @Test public void test1() throws IOException { //这里是一个输入流,用于加载SqlMapConfig.xml中的内容 InputStream inputStream=Resources.getResourceAsStream("SqlMapConfig.xml"); SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();//这个方法 //是用于获取数据流 SqlSessionFactory sqlsessionFactory=sqlSessionFactoryBuilder.build(inputStream);//工厂SqlSession //Factory主要是通过sqlSessionFactoryBuilder获取资源// 主要通过 SqlSession sqlSession=sqlsessionFactory.openSession();//获取sqlSession对象,所有的实例化对象都在SQLSession中 User user=sqlSession.selectOne(" com.iotek.getUserById") ; //为什么是selectOne?因为对象封装是一条记录,所以是selectOne //这里返回的是一个对象user //这条代码的意思是命名空间下的id方法,问题:这个ID方法可不可以随意写? System.out.println(user); } at com.iotek.mybatis.MybatisTest.test1(MybatisTest.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58) Caused by: org.apache.ibatis.builder.BuilderException: Error parsing SQL Mapper Configuration. Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xml.sax.SAXParseException; lineNumber: 2; columnNumber: 6; 不允许有匹配 "[xX][mM][lL]" 的处理指令目标。 }
切记:配置文件前面不能有空行,因为读取配置文件是从前面的读的,空行也算是字符,这个bug折磨了我几天
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。