黑马MyBatis入门day1

时间:2023-03-09 17:39:55
黑马MyBatis入门day1
 package com.itheima.domain;

 /*
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`birthday` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
*/
public class User {
private int id;
private String username;
private String password;
private Long birthday; public User() {
} 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 Long getBirthday() {
return birthday;
} public void setBirthday(Long birthday) {
this.birthday = birthday;
} @Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
'}';
}
}

User

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.itheima.mapper.UserMapper">
<!--查询全部-->
<select id="findAll" resultType="User">
select * from user
</select> <!--根据id进行查询-->
<select id="findById" resultType="User" parameterType="int">
select * from user where id=#{id}
</select> <!--增-->
<insert id="add" parameterType="User">
insert into user(username, password) values(#{username}, #{password})
</insert> <!--删-->
<delete id="delete" parameterType="java.lang.Integer">
delete from user where id=#{id}
</delete> <!--改-->
<update id="update" parameterType="User">
update user set username=#{username}, password=#{password} where id=#{id}
</update>
</mapper>

UserMapper.xml

 <?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> <!--外部的配置文件-->
<properties resource="jdbc.properties"></properties> <!--别名-->
<typeAliases>
<!--<typeAlias type="com.itheima.domain.User" alias="User"/>-->
<package name="com.itheima.domain"/>
</typeAliases> <!--配置数据源和事务管理-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment> <environment id="test">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3307/test" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments> <!--映射配置-->
<mappers>
<!--<mapper resource="com.itheima.mapper.UserMapper.xml"/>-->
<mapper resource="com/itheima/mapper/UserMapper.xml"/>
</mappers> </configuration>

SqlMapConfig

 jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/test
jdbc.username=root
jdbc.password=root

jdbc.properties

 package com.itheima;

 import com.itheima.domain.User;
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 org.junit.Test; import java.io.IOException;
import java.io.InputStream;
import java.util.List; public class CRUDTest { @Test
public void findAll() throws IOException {
// String config = "com/itheima/mapper/UserMapper.xml";
String config = "SqlMapConfig.xml";
InputStream resource = Resources.getResourceAsStream(config);
//根据xml配置创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行语句
List<User> userList = sqlSession.selectList("com.itheima.mapper.UserMapper.findAll");
for (User user : userList) {
System.out.println(user);
}
} @Test
public void findById() throws IOException {
//加载配置
String mybatisConfig = "SqlMapConfig.xml";
InputStream mybatisConfigStream = Resources.getResourceAsStream(mybatisConfig);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisConfigStream);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//语句
User user = sqlSession.selectOne("com.itheima.mapper.UserMapper.findById", 1);
System.out.println(user);
} @Test
public void add() throws IOException {
//准备对象
User user = new User();
user.setUsername("liubei");
user.setPassword("123"); // String config = "com/itheima/mapper/UserMapper.xml";
String config = "SqlMapConfig.xml";
InputStream resource = Resources.getResourceAsStream(config);
//根据xml配置创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resource);
//创建SqlSession
// SqlSession sqlSession = sqlSessionFactory.openSession();
SqlSession sqlSession = sqlSessionFactory.openSession(false);
//执行语句
int rows = sqlSession.insert("com.itheima.mapper.UserMapper.add", user);
//提交事务
// sqlSession.commit();
System.out.println(rows);
} @Test
public void delete() throws IOException {
String config = "SqlMapConfig.xml";
InputStream resourceStream = Resources.getResourceAsStream(config);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceStream);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//删除
int rows = sqlSession.delete("com.itheima.mapper.UserMapper.delete", 10);
//提交事务
sqlSession.commit();
System.out.println(rows);
} @Test
public void update() throws IOException {
//准备对象
User user = new User();
user.setId(1);
user.setUsername("root1");
user.setPassword("root"); //加载核心配置
String mybatisConfig = "SqlMapConfig.xml";
InputStream mybatisConfigStream = Resources.getResourceAsStream(mybatisConfig);
//创建SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(mybatisConfigStream);
//创建SqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
//语句
int rows = sqlSession.update("com.itheima.mapper.UserMapper.update", user);
//手动提交事务
sqlSession.commit();
System.out.println(rows);
}
}

CRUDTest

黑马MyBatis入门day1