CREATE DATABASE `mybatis` ;
USE `mybatis`;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
insert into `user`(`id`,`name`,`password`) values (1,'giao','admin'),(2,'giao2','321');
四、创建实体类
(Lombok没有用过的要先去idea的插件中心下载一个,直接搜索Lombok就好)
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data//生成get和set方法
@AllArgsConstructor//生成有参构造
@NoArgsConstructor//生成无参构造
public class User {
private int id;
private String name;
private String password;
}
五、创建mybatis的配置文件
六、创建Mapper接口,也就是Dao接口
(这张图是后来补的,爆红啥的不重要)
import java.util.List;
public interface UserMapper {
public int save(User user);
public int dalete(int id);
public List findAll();
public User findById(int id);
public int update(User user);
}
七、创建Mapper的实现配置文件
insert into user(name, password) VALUES (#{name},#{password})
delete from user where id=#{id}
update user set name=#{name},password=#{password} where id=#{id}
八、在mybatis的配置文件中注册mapper
(就加上最下面那几句就好啦)
九、创建测试类,就可以自己跑跑看啦,代码奉上了
(显示缺少啥包就自己根据idea的提示自动引入就好啦)
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
import java.util.List;
public class Test {
public static void main(String[] args) {
//通过类加载器加载配置文件
InputStream inputStream=Test.class.getClassLoader().getResourceAsStream("config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
//增
// User user=new User(2,"giao2","123");
// int save = mapper.save(user);
// sqlSession.commit();//提交事务,有数据变动必须提交事务
// System.out.println(save);
// sqlSession.close();//关闭sqlSession避免资源浪费
//删
// int delete = mapper.delete(3);
// sqlSession.commit();
// System.out.println(delete);
// sqlSession.close();
// 查询所有用户 注:有数据变动的才用commit提交事务,像查询的话是没有数据变动的,所以不用commit提交事务
// List users = mapper.findAll();
// for(User user:users){
// System.out.println(user);
// }
// sqlSession.close();
//通过id查找用户
// User user = mapper.findById(2);
// System.out.println(user);
// sqlSession.close();
//修改
// User user = new User(2, "giao2", "321");
// int update = mapper.update(user);
// sqlSession.commit();
// System.out.println(update);
// sqlSession.close();
}
}
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
For example,Given 1->2->3->3->4->4->5,