mybatis学习(15):mybatis连接mysql数据库

目录结构

com.geyao.mybatis.mapper

BlogMapper类

package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(String id);
}

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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀)-->
<mapper namespace="com.geyao.mybatis.mapper.BlogMapper"><!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回User类就是users表所对应的实体类--><!-- 根据id查询得到一个user对象--><select id="selectBlog"  resultType="Blog">select * from Blog where id=#{id}</select>
</mapper>

com.geyao.mybatis.pojo

Blog类

package com.geyao.mybatis.pojo;public class Blog {private String id;private String title;private int authod_id;private String state;private Boolean featured;private String style;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public int getAuthod_id() {return authod_id;}public void setAuthod_id(int authod_id) {this.authod_id = authod_id;}public String getState() {return state;}public void setState(String state) {this.state = state;}public Boolean getFeatured() {return featured;}public void setFeatured(Boolean featured) {this.featured = featured;}public String getStyle() {return style;}public void setStyle(String style) {this.style = style;}@Overridepublic String toString() {return "Blog [id=" + id + ", title=" + title + ", authod_id=" + authod_id + ", state=" + state + ", featured="+ featured + ", style=" + style + "]";}}

com.geyao.mybatis.util

MybatisUtil类

package com.geyao.mybatis.util;import java.io.InputStream;
import java.io.Reader;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MyBatisUtil {private static SqlSessionFactory sqlSessionFactory =null;static {try {InputStream in = Resources.getResourceAsStream("mybatis-config.xml");sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}private MyBatisUtil() {}public static SqlSession getSqlSession() {return sqlSessionFactory.openSession();}
}

log4j.properties

### \u914D\u7F6E\u6839 ###
log4j.rootLogger = debug,console ,fileAppender,dailyRollingFile,ROLLING_FILE,MAIL,DATABASE### \u8BBE\u7F6E\u8F93\u51FAsql\u7684\u7EA7\u522B\uFF0C\u5176\u4E2Dlogger\u540E\u9762\u7684\u5185\u5BB9\u5168\u90E8\u4E3Ajar\u5305\u4E2D\u6240\u5305\u542B\u7684\u5305\u540D ###
log4j.logger.org.apache=dubug
log4j.logger.java.sql.Connection=dubug
log4j.logger.java.sql.Statement=dubug
log4j.logger.java.sql.PreparedStatement=dubug
log4j.logger.java.sql.ResultSet=dubug### \u914D\u7F6E\u8F93\u51FA\u5230\u63A7\u5236\u53F0 ###
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =  %d{ABSOLUTE} %5p %c{1}:%L - %m%n

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><typeAliases><typeAlias type="com.geyao.mybatis.pojo.Blog" alias="Blog"/>
</typeAliases><environments default="development"><environment id="development"><transactionManager type="JDBC" /><!-- 配置数据库连接信息 --><dataSource type="POOLED"><property name="driver" value="com.mysql.cj.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT%2B8" /><property name="username" value="root" /><property name="password" value="123" /></dataSource></environment></environments><mappers><!-- 注册userMapper.xml文件, userMapper.xml位于me.gacl.mapping这个包下,所以resource写成me/gacl/mapping/userMapper.xml--><mapper resource="com/geyao/mybatis/mapper/BlogMapper.xml"/></mappers>
</configuration>

单元测试

com.geyao.mybatis.util

testSelectBlog类

package com.geyao.mybatis.mapper;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import com.geyao.mybatis.pojo.Blog;
import com.geyao.mybatis.util.MyBatisUtil;public class testSelectBlog {
@Test
public void testSelectBlog() {SqlSession session =MyBatisUtil.getSqlSession();BlogMapper blogMapper =session.getMapper(BlogMapper.class);Blog blog = blogMapper.selectBlog("1");System.out.println(blog);
}
}

jar包

链接:https://pan.baidu.com/s/1g6NgzfLc5uK9S4VL-03lHg
提取码:4r2m

运行结果

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/423844.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

nodejs对mongodb数据库的增删改查操作(转载)

首先要确保mongodb的正确安装&#xff0c;安装参照&#xff1a;http://docs.mongodb.org/manual/tutorial/install-mongodb-on-debian-or-ubuntu-linux 另外可以参考我的另一篇博文 http://www.cnblogs.com/sexintercourse/p/5774310.html 指导mongo和nodejs的开发 然后下载nod…

mybatis学习(16):不使用接口的方式

目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Inteage id); }BlogMapper.xml <?xml version"1.0" encoding"UTF-8" ?>…

AS3图像抖动效果源码。

DemoSourceWebsite转载于:https://www.cnblogs.com/xxcainiao/archive/2008/05/07/1186805.html

mybatis学习(17):列名与属性名不一致的情况(使用别名)

目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Inteage id); }BlogMapper.xml <?xml version"1.0" encoding"UTF-8" ?>…

mybatis学习(18):列名与属性名不一致的情况(使用ResultMap)

目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id); }BlogMapper.xml <?xml version"1.0" en…

MVCToolKit中HtmlHelper.ActionLink的BUG?

分页时<%if (ViewData.Accounts.IsPreviousPage) { %><%Html.ActionLink<SpeakOutFreely.Controllers.ManageController>(c >c.List(ViewData.Accounts.PageIndex -1), "Previous")%><%} %><%if (ViewData.Accounts.IsNextPage) { %&g…

PyChram简单使用教程

一、PyChram下载 官网&#xff1a;http://www.jetbrains.com/pycharm Windows:http://www.jetbrains.com/pycharm/download/#sectionwindows Linux:http://www.jetbrains.com/pycharm/download/#sectionlinux Mac:http://www.jetbrains.com/pycharm/download/# 二、PyChram简单…

mybatis学习(19):模糊查询#

目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTi…

mybatis学习(20):模糊查询$

目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> selectBlogByTi…

mybatis学习(21):MySQL 字符串 转换 CAST与CONVERT 函数的用法

MySQL 的CAST()和CONVERT()函数可用来获取一个类型的值&#xff0c;并产生另一个类型的值。两者具体的语法如下&#xff1a; Sql代码 CAST(value as type); CONVERT(value, type); 就是CAST(xxx AS 类型), CONVERT(xxx,类型)。 Sql代码 mysql> SELECT CAST(3.35 …

【转载】快速、可伸缩和流式的AJAX代理--跨域持续内容分发

原文&#xff1a;《Fast, Scalable, Streaming AJAX Proxy - continuously deliver data from across domains》 作者&#xff1a;Omar Al ZabirURL&#xff1a; http://www.codeproject.com/KB/ajax/ajaxproxy.aspx Download source - 16.1 KB Introduction Due to browsers p…

pycharm常用设置(keymap设置及eclipse常用快捷键总结)

2015-04-15 13:23 23800人阅读 评论(0) 收藏 举报分类&#xff1a;openstack-环境及安装配置&#xff08;10&#xff09; 版权声明&#xff1a;欢迎大家转载&#xff0c;转载请注明出处blog.csdn.net/tantexian。 设置pycharm为Eclipse快捷键后使用总结&#xff1a; Ctrl O 根…

mybatis学习(22):查询排序

按照某列排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> …

python基础:python循环、三元运算、字典、文件操作

目录&#xff1a; python循环三元运算字符串字典文件操作基础 一、python编程 在面向过程式编程语言的执行流程中包含&#xff1a; 顺序执行 选择执行 循环执行 if是条件判断语句&#xff1b;if的执行流程属于选择执行&#xff1b;if语句有三种格式&#xff0c;如下&#xff1…

mybatis学习(23):分页1 多参数传递(索引方式)

分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog selectBlog2(Integer id);List<Blog> sele…

mybatis学习(24):分页2 多参数传递(使用注解)

分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBlog(Integer id);Blog se…

Python -- 三元表达式(三目运算符)

一些语言&#xff08;如Java&#xff09;的三元表达式形如&#xff1a; [plain] view plaincopy 判定条件?为真时的结果:为假时的结果 Python的三元表达式有如下几种书写方法&#xff1a; [python] view plaincopy if __name__ __main__: a b True c …

Vista SP1、IIS7,安装ASP.Net 1.1、VS2003、NetAdvantage 2004vol、Sql Server2000全攻略

对于微软的软件&#xff0c;一向认为是&#xff1a;beta版惨不忍睹&#xff0c;rtm版马马虎虎&#xff0c;sp1版。所以&#xff0c;现在Vista SP1出来了&#xff0c;我也考虑更换操作系统了。经过一番google&#xff0c;唯一的问题应该就在于ASP.Net 1.1在Vista上的运行调试了&…

mybatis学习(25):分页3 多参数传递(使用map)

分页排序 目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import java.util.List; import java.util.Map;import org.apache.ibatis.annotations.Param;import com.geyao.mybatis.pojo.Blog;public interface BlogMapper {Blog selectBl…

2019CCPC湖南全国邀请赛-Chika and Friendly Pairs- 莫队+树状数组+离散化

题目链接&#xff1a; http://acm.hdu.edu.cn/showproblem.php?pid6534 思路&#xff1a; 莫队算法&#xff0c;树状数组求和。每个数对应的离散化之后的值需要打表存下来。 Code&#xff1a; 1 #include<bits/stdc.h>2 using namespace std;3 const int N2700010;4 int…