下載app免費(fèi)領(lǐng)取會(huì)員
1.創(chuàng)建控制臺(tái)程序。
2.導(dǎo)入mysql和mybatis的jar包
3.創(chuàng)建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.創(chuàng)建實(shí)體類
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.創(chuàng)建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函數(shù)
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("趕緊去數(shù)據(jù)庫查查?。?!");
}
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("麻溜的去數(shù)據(jù)庫查查?。?!");
}
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("...");
}
}
本文版權(quán)歸腿腿教學(xué)網(wǎng)及原創(chuàng)作者所有,未經(jīng)授權(quán),謝絕轉(zhuǎn)載。
上一篇:二次開發(fā)教程:研究下WPF 數(shù)據(jù)binding 原理
推薦專題