Eclipse利用Maven快速上手搭建MyBatis
一、what is maven?
maven项目对象模型(pom),可以通过一小段描述信息来管理项目的构建,报告和文档的项目管理工具软件。
二、安装与配置
2.1、直接下载(需jdk1.7或更高)
地址:
2.2、官网下载:http://maven.apache.org/download.cgi
2.3、maven与eclipse关联
2.4、创建maven项目,并配置pom.xml
1 <dependencies> 2 <!-- 添加mybatis框架3.4.6版本 --> 3 <dependency> 4 <groupid>org.mybatis</groupid> 5 <artifactid>mybatis</artifactid> 6 <version>3.4.6</version> <!-- 版本号视情况修改 --> 7 </dependency> 8 <!-- 添加mysql驱动包 --> 9 <dependency> 10 <groupid>mysql</groupid> 11 <artifactid>mysql-connector-java</artifactid> 12 <version>5.1.25</version> 13 </dependency> 14 </dependencies>
2.5、创建xml配置mybatis
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/sam" /> <!-- 连接字符串 --> 12 <property name="username" value="root" /> <!-- 用户名 --> 13 <property name="password" value="root" /> <!-- 密码 --> 14 </datasource> 15 </environment> 16 </environments> 17 <mappers> 18 <mapper resource="deptmapper.xml" /> <!-- 映射sql语句的xml文件 --> 19 </mappers> 20 </configuration>
2.6、创建xml映射sql语句
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 <mapper namespace="dept"> 6 <!-- 插入单个部门信息 --> 7 <insert id="insertdept"> 8 insert into dept (dname,loc) 9 values (#{dname},#{loc}) 10 </insert> 11 </mapper>
crud语法
1 <insert id="insertauthor"> 2 insert into author (id,username,password,email,bio) 3 values (#{id},#{username},#{password},#{email},#{bio}) 4 </insert> 5 6 <update id="updateauthor"> 7 update author set 8 username = #{username}, 9 password = #{password}, 10 email = #{email}, 11 bio = #{bio} 12 where id = #{id} 13 </update> 14 15 <delete id="deleteauthor"> 16 delete from author where id = #{id} 17 </delete>
2.7、创建实体类
表结构
1 package com.chenyanbin; 2 3 public class dept { 4 //部门名称 5 private string dname; 6 //部门位置 7 private string loc; 8 public string getdname() { 9 return dname; 10 } 11 public void setdname(string dname) { 12 dname = dname; 13 } 14 public string getloc() { 15 return loc; 16 } 17 public void setloc(string loc) { 18 loc = loc; 19 } 20 }
2.8、创建main函数
1 package com.chenyanbin; 2 3 import java.io.ioexception; 4 import java.io.inputstream; 5 import org.apache.ibatis.io.resources; 6 import org.apache.ibatis.session.sqlsession; 7 import org.apache.ibatis.session.sqlsessionfactory; 8 import org.apache.ibatis.session.sqlsessionfactorybuilder; 9 10 public class testmain { 11 public static void main(string[] args) throws ioexception { 12 //创建实体类 13 dept dept = new dept(); 14 dept.setdname("上海事业部"); 15 dept.setloc("上海"); 16 //加载xml文件 17 inputstream is = resources.getresourceasstream("mybatis-config.xml"); //加载mybatis的配置文件 18 //初始化sqlsessionfactory 19 sqlsessionfactory factory = new sqlsessionfactorybuilder().build(is); 20 sqlsession session = factory.opensession(); 21 session.insert("insertdept", dept); 22 session.commit(); 23 session.close(); 24 } 25 }
2.9、项目文件目录图
以上配置完成,但是博主碰到一个问题,数据库保存进去了,程序警告,警告如下:
warning: an illegal reflective access operation has occurred
warning: illegal reflective access by org.apache.ibatis.reflection.reflector (file:/c:/users/windows10/.m2/repository/org/mybatis/mybatis/3.4.6/mybatis-3.4.6.jar) to method java.lang.class.checkpackageaccess(java.lang.securitymanager,java.lang.classloader,boolean)
warning: please consider reporting this to the maintainers of org.apache.ibatis.reflection.reflector
warning: use --illegal-access=warn to enable warnings of further illegal reflective access operations
warning: all illegal access operations will be denied in a future release
网上查了下,jdk8之后对反射做限制了,有两种解决方案
- 把jdk回到jdk9之前
- 升级mybatis
上一篇: 什么是昆仑奴?唐朝贵族们为何爱不释手呢?
下一篇: 同样是功高震主,王翦的结局如何?