下载app免费领取会员
1.创建控制台程序。
2.导入mysql和mybatis的jar包
3.创建mybatis配置文件
<?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 alias="User" type="models.User" />
</typeAliases>
<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://127.0.0.1:3306/test?useSSL=true" />
<property name="username" value="root" />
<property name="password" value="" />
</dataSource>
</environment>
</environments>
</configuration>
4.创建实体类
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public User(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
5.创建IUser接口
public interface IUser {
@Select("select * from tt where id =#{id}")
public User getUserById(int id);
@Select("select * from tt")
public List<User> getUserList();
@Insert("insert into tt(id,name) values(#{id},#{name})")
public void insertUser(User user);
@Delete("delete from tt where id = #{id}")
public void deleteUserById(int id);
@Update("update tt set name=#{name} where id =#{id}")
public void updateUserById(User user);
}
6.main函数
public class Main {
private static SqlSessionFactory sqlSessionFactory;
private static Reader reader;
static {
try {
reader = Resources.getResourceAsReader("mybatis.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
sqlSessionFactory.getConfiguration().addMapper(IUser.class);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//testSelect();
//testInsert();
//testDelete();
testUpdate();
}
private static void testSelect(){
System.out.println("test select...");
SqlSession session = sqlSessionFactory.openSession();
try {
IUser iUser = session.getMapper(IUser.class);
List<User> users = iUser.getUserList();
users.forEach(m -> {
System.out.println(m);
});
}finally {
session.close();
}
}
private static void testInsert(){
System.out.println("test insert...");
SqlSession session = sqlSessionFactory.openSession();
try {
IUser iUser = session.getMapper(IUser.class);
User testUser = new User(7, "name7");
iUser.insertUser(testUser);
session.commit();
}
finally {
session.close();
}
System.out.println("赶紧去数据库查查!!!");
}
private static void testDelete(){
System.out.println("test delete ...");
SqlSession session = sqlSessionFactory.openSession();
try {
IUser iUser = session.getMapper(IUser.class);
iUser.deleteUserById(7);
session.commit();
}finally {
session.close();
}
System.out.println("麻溜的去数据库查查!!!");
}
private static void testUpdate(){
System.out.println("test update...");
SqlSession session = sqlSessionFactory.openSession();
try {
IUser iUser = session.getMapper(IUser.class);
User testUser = new User(5, "name5");
iUser.updateUserById(testUser);
session.commit();
}
finally {
session.close();
}
System.out.println("...");
}
}
本文版权归腿腿教学网及原创作者所有,未经授权,谢绝转载。
上一篇:二次开发教程:研究下WPF 数据binding 原理
下一篇:二次开发教程:MyBatis配置