关于如何快速创建 Maven 项目,这个可以参考下面这篇文章:
Maven 项目管理工具基础入门系列(一)
打开 pom.xml
文件,快速配置 jar 包,配置代码如下:
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4.0.0
com.mybatis
MyBatisDemo
war
0.0.1-SNAPSHOT
MyBatisDemo Maven Webapp
http://maven.apache.org
org.mybatis
mybatis
3.4.5
mysql
mysql-connector-java
5.1.25
junit
junit
3.8.1
test
MyBatisDemo
src/main/java
**/*.xml
true
创建 pojo 类 User.java
,代码如下:
package com.mybatis.pojo;
public class User{
private int id;
private String username;
private String password;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password="
+ password + ", age=" + age + "]";
}
}
新建数据库 mybatis_db,创建表 t_user,建表代码如下:
CREATE TABLE `t_user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL,
`password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '',
`age` int(11) NULL DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 2 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;
创建接口 UserDao.java
,代码如下:
package com.mybatis.dao;
import com.mybatis.pojo.User;
public interface UserDAO {
//新增用户
public int addUser(User user);
//根据id删除用户
public int deleteUser(int id);
//修改用户
public int updateUser(User user);
//根据id查询用户
public User selectUserById(int id);
}
创建 xml 文件 UserDaoMapper.xml
,代码如下:
<mapper namespace="com.mybatis.dao.UserDao">
<insert id="addUser" parameterType="com.mybatis.pojo.User">
insert into t_user (username,password,age) values (#{username},#{password},#{age})
insert>
<delete id="deleteUser" parameterType="int">
delete from t_user where id=#{id}
delete>
<update id="updateUser" >
update t_user set username=#{username},password=#{password},age=#{age} where id=#{id}
update>
<select id="selectUserById" parameterType="int" resultType="com.mybatis.pojo.User">
select * from t_user where id=#{id}
select>
mapper>
创建 xml 文件 mybatis-comfig.xml
,具体配置如下:
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mybatis_db?useUnicode=true&characterEncoding=UTF-8" />
<property name="username" value="root" />
<property name="password" value="" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="com/mybatis/dao/UserDaoMapper.xml"/>
mappers>
configuration>
创建测试类 Test.java
,代码如下:
package com.mybatis.test;
import java.io.InputStream;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.mybatis.dao.UserDao;
import com.mybatis.pojo.User;
public class Test {
public static void main(String[] args) {
//加载MyBatis配置文件
InputStream is = Test.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//获取SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//获取实现接口的代理对象
UserDao userDAO = sqlSession.getMapper(UserDao.class);
//新增用户
User user = new User();
user.setUsername("孔乙己");
user.setPassword("123456");
user.setAge(21);
System.out.println(userDAO.addUser(user));
sqlSession.commit();
// //删除用户
// System.out.println(userDAO.deleteUser(1));
// sqlSession.commit();
//
// //查询用户
// User user1 = userDAO.selectUserById(1);
// System.out.println(user1);
//
// //修改用户
// User user2 = userDAO.selectUserById(1);
// user2.setUsername("指南者");
// System.out.println(userDAO.updateUser(user2));
// sqlSession.commit();
}
}
本程序和数据库相关的代码已经上传到 GitHub 上,GitHub 地址:点击这里直接获取项目源码