mybatis简单入门实例
来源:互联网 发布:华为软件视频会议 编辑:程序博客网 时间:2024/05/21 17:24
最近刚接触到mybatis这个orm框架,之前接触过hibernate,不过想吐槽的是hibernate真的比较重量级,开发比较麻烦,所以再来看看mybatis怎么样。
以下是上手小实例。
1.环境部署:引入相关jar包(这里介绍个网站MavenRepository,http://mvnrepository.com ,可以在上面搜寻需要的maven依赖)
2.相关配置文件的配置
(1).Mybatis主配置文件—–Configure.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="mybatis/config.properties"/> <!-- 为JAVA Bean起类别名,简化开发 --> <typeAliases > <package name="vip.ntwxhn.model"/> </typeAliases> <!-- 配置mybatis运行环境 ,注意:在mybatis和spring整合后environments的配置会省略--> <environments default="development"> <environment id="development"> <!--采用jdbc的事务提交--> <transactionManager type="JDBC" /> <!-- POOLED 表示支持JDBC数据源连接池 --> <!-- UNPOOLED 表示不支持数据源连接池 --> <!-- JNDI 表示支持外部数据源连接池 --> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </dataSource> </environment> </environments> <mappers> <!--mapper标签配置的是映射文件--> <mapper resource="mappers/User.xml"/> </mappers></configuration>
(2)Configure.xml中引用的config.properties
jdbc.driver=com.mysql.cj.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTCjdbc.username=rootjdbc.password=root
注:jdbc.url的配置看着好像过长,但是后面的参数都是有用的,解决了我的程序运行时的一个异常,如下:
java.sql.SQLException: The server time zone value '�й���ʱ��' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
这个错误是因为mysql连接的依赖包是高版本的情况下,在配置jdbc.url不能像jdbc:mysql://localhost:3306/blog这样简单的配::
得像下面这样配
jdbc.url=jdbc:mysql://localhost:3306/blog?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
(3)映射文件UserMapper.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"><!--sql语句结尾不加分号--><!--sql语句中#{}代表占位符,没有sql注入的危险;${}是拼接符,有sql注入的危险--><mapper namespace="test"> <select id="query" parameterType="java.lang.String" resultType="vip.ntwxhn.model.User"> SELECT * FROM USER WHERE username LIKE '%${value}%' </select> <!--插入时参数类型为pojo--> <insert id="add" parameterType="vip.ntwxhn.model.User" > INSERT INTO USER(userId,username) VALUES(#{userId},#{username}); </insert></mapper>
(4)当然还得有实体类User
package vip.ntwxhn.model;/** * Created by user on 2017/11/17. */public class User { private String userId; private String username; private String password; private String email; private String phonenumber;setter,getter省略.....
(5)测试类Test
package vip.ntwxhn.service;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 vip.ntwxhn.model.User;import java.io.IOException;import java.io.InputStream;import java.util.List;/** * Created by user on 2017/11/5. */public class Test { public static void main(String[] args) throws IOException { InputStream resourceAsStream = Resources.getResourceAsStream("mybatis/Configure.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream); SqlSession sqlSession = sqlSessionFactory.openSession();// User user = new User();// user.setUserId("6");// user.setUsername("dfg");// sqlSession.insert("test.add",user);// sqlSession.commit(); User user = sqlSession.selectOne("test.query", "1"); System.out.println(user); sqlSession.close(); }}
以上代码都是运行过再贴的,如果想粘贴的话,请放心。
阅读全文
0 0
- mybatis简单入门实例
- Mybatis入门最简单实例
- 【MyBatis学习01】一个简单入门实例
- spring mvc 和 mybatis整合,简单的入门实例
- MyBatis经典入门实例
- MyBatis经典入门实例
- MyBatis经典入门实例
- mybatis入门实例
- mybatis入门实例
- MyBatis入门实例简介
- MyBatis入门实例
- MyBatis经典入门实例
- MyBatis入门实例简介
- Mybatis入门实例
- MyBatis经典入门实例
- MyBatis上手入门实例
- Mybatis入门实例(一)
- mybatis 入门实例
- C++栈和队列
- LYSpython3.0学习笔记(1)
- 服务端数据推送方式-SSE(server-sent event)
- android开发标题栏
- mongoDB的常用语法
- mybatis简单入门实例
- 笛卡尔积实现(递归C++版本)
- Android开发百科全书②
- Javascript知识点总结(八)
- Anaconda下载地址
- 设计模式概述(转载)
- Anaconda3 配置TensorFlow,opencv,keras
- 我形我速7中文免费版下载附安装教程
- 请记住编程中的24条经典语录