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项目搭建过程,包括里面步骤和里面的配置文件如何配置等等都会详细说明. 接下来马上进入项目搭建 ...
随机推荐
- jvm学习:类的加载、连接、初始化、常量
类在jvm中有这几个过程类的加载.连接.初始化.使用.卸载 类的加载 类的加载是将class文件中的二进制数据加载到内存中,将其放在运行时的数据区:方法区内,然后在内存中创建一个 java.lang. ...
- python绘制疫情图
python中进行图表绘制的库主要有两个:matplotlib 和 pyecharts, 相比较而言: matplotlib中提供了BaseMap可以用于地图的绘制,但是个人觉得其绘制的地图不太美观, ...
- 利用TPL(任务并行库)构建Pipeline处理Dataflow
https://www.cnblogs.com/CoderAyu/p/9757389.html
- redis之Hash类型常用方法总结
redis之Hash类型常用方法总结 格式: 存--HMGET key field [field ...] 取--HMGET key field [field ...] M:表示能取多个值,many ...
- Python学习笔记之基础篇(五)字典
#数据类型划分:可变数据类型 不可变数据类型 #不可变数据类型 : 元组 bool int str --> 可哈希 #可变数据类型 list ,dict set --->不可哈希 ''' ...
- Linux命令:date命令
date命令作用:显示和设置系统的日期和时间 一.设置系统日期时间 格式:date [MMDDhhmm[[CC]YY][.ss]] 举例:将当前系统时间改为 2020年10月1日12点10分 # da ...
- AJAX的表单请求POST请求方式
表单数据的提交 action : 数据提交的地址,默认是当前页面 method : 数据提交的方式,默认是get方式 post: 把数据名称和数据值用=连接,如果有多个的话,那么他会把多个数据组合用& ...
- Day3-P - Matrix POJ3685
Given a N × N matrix A, whose element in the i-th row and j-th column Aij is an number that equals i ...
- Redis散列表类型
散列类型(hash)的键值也是一种字典结构,其存储了字段(field)和字段值的映射,但字段值只能是字符串,不支持其他的数据类型. 一个散列类型键可以包含至多2^32 -1个字段. 命令 赋值 HSE ...
- python表白实现代码(可视化与动画版)
python表白实现代码(可视化与动画版)如何优雅而又高大上地对自己的心爱女神表白了? ? ? 试试python表白的实现方式吧,是动画版的哦,保证可以如你所愿 ! ! !最终的实现效果如下: 具体实 ...