标签:jdbc Demo class mybatis 源码 mysql Mybatis log4j
1 源码下载
到mybatis的官网进行源码下载:https://mybatis.org/mybatis-3/,
我们选择Source code下载
下载后解压,该项目是一个maven项目,我们用idea打开,目录结构如下
2 Demo项目
我们在源码工程下创建一个demo目录,存放我们的demo代码
MybatisMain.java
public class MybatisMain { public static void main(String[] args) throws IOException { String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession sqlSession = sqlSessionFactory.openSession(); Blog blog = sqlSession.selectOne("org.mybatis.example.BlogMapper.selectBlog", 101); System.out.println(blog.getContext()); } }
Blog.java
public class Blog { private Integer id; private String username; private String context; //省去了getter和setter }
mybatis-config.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> <properties resource="jdbc.properties"/> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/BlogMapper.xml"/> </mappers> </configuration>
jdbc.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=root
BlogMapper.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="org.mybatis.example.BlogMapper"> <select id="selectBlog" resultType="org.apache.ibatis.demo.Blog"> select * from Blog where id = #{id} </select> </mapper>
最后需要注意的是我们要把pom文件中的如下部分注释掉,要不然运行会报错。
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.24</version> <!--<scope>test</scope>注释掉--> </dependency>
同时,我们要有一个mysql实例,配置见jdbc.properties
mysql> use test Database changed mysql> select * from blog; +------+----------+---------+ | id | username | context | +------+----------+---------+ | 101 | szj | hello | +------+----------+---------+ 1 row in set (0.00 sec) mysql>
运行结果
"C:\Program Files (x86)\Java\jdk1.8.0_181\bin\java" ... log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary. hello Process finished with exit code 0
下一节我们将剖析以上示例的运行原理
标签:jdbc,Demo,class,mybatis,源码,mysql,Mybatis,log4j 来源: https://www.cnblogs.com/zhenjingcool/p/15640977.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。