maven项目使用mybatis+mysql
1.添加依赖,在pom.xml中添加
<!--mybatis核心包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<!--MySQL的JDBC驱动包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.29</version>
</dependency>
2. 添加Entity
public class UsersEntity {
private Integer userid;
private String username;
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
}
3. 添加dao层的interface UsersEntityMapper.java
public interface UsersEntityMapper {
UsersEntity getUsersByPhoneNo(String phoneNo);
}
4. 添加mapper的xml UsersEntityMapper.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="com.alphaomega.sunshine.dao.UsersEntityMapper">
<select id="getUsersByPhoneNo" parameterType="String" resultType="com.alphaomega.sunshine.entity.UsersEntity">
select * from users where phoneno = #{phoneNo}
</select>
</mapper>
5. 添加mybatis的配置文件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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<!-- 1.加载数据库驱动 -->
<property name="driver" value="com.mysql.jdbc.Driver" />
<!-- 2.数据库连接地址 (&->&)-->
<property name="url" value="jdbc:mysql://127.0.0.1:3306/schema_name?useUnicode=true&characterEncoding=utf-8&useSSL=true" />
<!-- 数据库用户... -->
<property name="username" value="root" />
<!-- 数据库密码... -->
<property name="password" value="xxxxxxx" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册文件-->
<mapper resource="com/alphaomega/sunshine/mappers/UsersEntityMapper.xml"/>
<mapper resource="com/alphaomega/sunshine/mappers/XxxxxxEntityMapper.xml"/>
</mappers>
</configuration>
6. mybatis配置文件mybatis_config.xml和UsersEntityMapper.xml的位置
UsersEntityMapper.xml如果放在src/main/java里的话,则需要向pom添加路径,否则package时会报错:找不到xml文件,
原因是maven默认xml文件在src/main/resources里(不过后来发现,即使放在了默认里也不行)。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
向maven项目添加src/main/resources目录,并将mybatis_config.xml放在里面。(虽然可以通过另创建文件夹,然后use as source,但还是按照maven默认比较舒服)
添加的方式可以通过Project-Properties-Java Build Path-Source-Add Folder-选中文件夹后Create new folder...
同样,测试目录src/test/resources也通过这样执行。
问题来了,测试时没有任何问题,但是jar包不能,maven会把test目录的xml文件复制到target/test-class下,但是不会将main下的xml复制到target/class里,
除了那些mapper,因为已经指定了路径。因此还需要在pom的resources标签里指定,为了maven复制过去。
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
7. 最后使用
String resouces = "mybatis_config.xml";
InputStream in = Resources.getResourceAsStream(resouces);
//读取配置文件的配置信息,利用SqlSessionFactoryBuilder创建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
//利用sqlSessionFactory打开与数据库的会话
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UsersEntityMapper ueMapper = sqlSession.getMapper(UsersEntityMapper.class);//通过sqlSession得到mapper
String no = "13299999999";
UsersEntity user = ueMapper.getUsersByPhoneNo(no);//调用mapper的方法
//只读操作不需要sqlSession.commit()
System.out.println("查询到用户...");
System.out.println(user.getUsername());
} finally {
sqlSession.close();
}
maven项目使用mybatis+mysql的更多相关文章
- 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+SpringMVC项目详解
http://blog.csdn.net/noaman_wgs/article/details/53893948 利用Intellij+MAVEN搭建Spring+Mybatis+MySql+Spri ...
- Maven项目使用mybatis报错 org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
maven项目使用mybatis时,找不到mapper文件(.xml) 错误信息提示: 项目可以正常运行,但是在有请求到达服务器时(有访问数据库的请求),会出现报错!! 错误原因: mybatis没有 ...
- springmvc学习总结(二) -- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
@_@ 写在最前 之前分享过下面这几篇: mybatis学习笔记(五) -- maven+spring+mybatis从零开始搭建整合详细过程(上)(附demo和搭建过程遇到的问题解决方法) myba ...
- maven+springmvc+spring+mybatis+mysql详细搭建整合过程讲解
转自:https://www.cnblogs.com/lmei/p/7190755.html?utm_source=itdadao&utm_medium=referral @_@ 写在最前 之 ...
- Maven 项目使用mybatis的环境搭建-基于xml形式实现查询所有的功能
首先了解一下什么是 MyBatis? MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集. ...
- idea 下 启动maven项目,mybatis报错 Error parsing SQL Mapper Configuration. Cause: java.io.IOException。。。。。
我的具体报错日志是 Error parsing SQL Mapper Configuration. Cause: java.io.IOException Could not find resou ...
- Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建(转)
这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 如果还没有搭建好环境( ...
- freemarker + spring mvc + spring + mybatis + mysql + maven项目搭建
今天说说搭建项目,使用freemarker + spring mvc + spring + mybatis + mysql + maven搭建web项目. 先假设您已经配置好eclipse的maven ...
- eclipse下SpringMVC+Maven+Mybatis+MySQL项目搭建
这篇文章主要讲解使用eclipse对Spirng+SpringMVC+Maven+Mybatis+MySQL项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 接下来马上进入项目搭建 ...
随机推荐
- Python学习笔记008
while循环 while 条件 : 执行 num =1 while num<=10: print(num) num+=1 1-100偶数 方法1 num =2 while num& ...
- Mark Grover
https://www.ibm.com/developerworks/cn/data/library/bd-zookeeper/
- SRS源码——Listener
1. 整理了一下Listener相关的UML类图:
- java 调用阿里云短信接口,报InvalidTimeStamp.Expired : Specified time stamp or date value is expired.
官网解释: 问题所在: 自己的电脑(或者服务器) 的时间与阿里云的服务器时间 相差15分钟了. 解决方法 : 把自己的电脑时间 (或者服务器)的时间 改成标准的北京时间就行了.
- 嵊州普及Day6T3
题意:n个点,对于q个询问,有t秒及一个矩形的范围.在此矩形内的数每秒加1,若等于c,则下一秒变为0. 思路:t可能很大,%c+1就可以了.然后一个一个加起来就可以了. 见代码: #include&l ...
- 搜索控制器UISearchController的使用
说明:本文主要是将系统UISearchController控件声明文件中的属性.方法.代理等介绍下,后续再联合[搜索栏UISearchBar]这篇文章一起更新更实用的信息. 1.初始化 // 如果设置 ...
- 一个Win32程序的进化------转载
一个Win32程序的进化 转载 weixin_34273046 发布于2017-11-15 10:53:00 阅读数 10 收藏 展开 一.为什么要学Win32 要回答这个问题,我们就要先搞清 ...
- VueCli3 项目结构和具体作用
- java程序员的就业指导(重点)
想要成为合格的Java程序员或工程师到底需要具备哪些专业技能,面试者在面试之前到底需要准备哪些东西呢?本文陈列的这些内容既可以作为个人简历中的内容,也可以作为面试的时候跟面试官聊的东西,你可以把这些内 ...
- Java中的输入"scanf",Scanner用法
1.输入程序 1)Scnner的初始用法: 1 import java.util.Scanner; 2 public class Textone { 3 public static void main ...