01.基于IDEA搭建mybatis的MAVEN项目
武都网站制作公司哪家好,找创新互联建站!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。创新互联建站从2013年创立到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联建站。
基础环境
- IDEA
- MAVEN
工程目录
MySQL数据库
Maven Pom
添加mybatis以及mysql的依赖包
4.0.0
com.mu
mybatis
1.0-SNAPSHOT
org.mybatis
mybatis
3.4.1
mysql
mysql-connector-java
5.1.29
src/main/resources
**/*.properties
**/*.xml
**/*.tld
false
src/main/java
**/*.properties
**/*.xml
**/*.tld
false
创建各实体类以及mapper.xml文件
- User.java
package com.mu.mybatis.domain;
/**
* Created by manunited1985 on 2017/11/23.
*/
public class User {
private Integer userId;
private String userName;
private String userPassword;
private String userEmail;
public User(Integer userId, String userName, String userPassword, String userEmail) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.userEmail = userEmail;
}
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;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userPassword=" + userPassword + ", userEmail=" + userEmail
+ "]";
}
}
- UserMapper.java
package com.mu.mybatis.mapper;
import com.mu.mybatis.domain.User;
/**
* Created by manunited1985 on 2017/11/24.
*/
public interface UserMapper {
public User selectUserById(Integer userId);
public List selectUserAll();
public void insertUser(User user);
}
- UserMapper.xml
insert into t_user (USER_ID,USER_NAME,USER_PASSWORD,USER_EMAIL)
values (#{userId},#{userName},#{userPassword},#{userEmail})
- Main.java
package com.mu.mybatis;
import com.mu.mybatis.domain.User;
import com.mu.mybatis.mapper.UserMapper;
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 java.io.InputStream;
import java.io.IOException;
import java.util.List;
/**
* Created by manunited1985 on 2017/11/23.
*/
public class Main {
public static void main(String[] args) throws IOException {
SqlSession session = null;
try {
// 读取配置信息
String resource = "application.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
// 选择不同的环境
String env = "product";
SqlSessionFactory factory = builder.build(inputStream,env);
// 产生SqlSession对象
session = factory.openSession();
System.out.println(session);
// 获取映射对象
UserMapper userMapper = session.getMapper(UserMapper.class);
// 获取id为1的账户
User user = userMapper.selectUserById(1);
System.out.println(user);
// 获取所有账户
List arrayList = userMapper.selectUserAll();
System.out.println(arrayList);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (session != null)
// 关闭连接
session.close();
}
}
}
数据库配置 jdbc.properties
jdbc_driver=com.mysql.jdbc.Driver
jdbc_url=jdbc\:mysql\://127.0.0.1\:3306/test?useUnicode=true&characterEncoding=utf-8
jdbc_username=root
jdbc_password=123456
应用配置application.xml
运行结果
org.apache.ibatis.session.defaults.DefaultSqlSession@454b70a9
User [userId=1, userName=xiaoming, userPassword=123456, userEmail=x@sina.com]
[User [userId=1, userName=xiaoming, userPassword=123456, userEmail=x@sina.com], User [userId=2, userName=tom, userPassword=123, userEmail=tom@sina.com], User [userId=3, userName=jack, userPassword=123, userEmail=jack@sina.com]]
备注
如果你利用IDEA创建的工程,在加载mapper.xml文件的时候,出现类似现象:
mybatis错误——java.io.IOException: Could not find resource com/xxx/xxxMapper.xml
那么,你可以在pom.xml文件的添加如下字段:
src/main/java
**/*.xml
IDEA是不会编译src的java目录的xml文件,所以在Mybatis的配置文件中找不到xml文件!(也有可能是Maven构建项目的问题,网上教程很多项目是普通的Java web项目,所以可以放到src下面也能读取到)
分享名称:01.基于IDEA搭建mybatis的MAVEN项目
网页链接:http://cdiso.cn/article/podhci.html