首先温故而知新,回顾以前的知识,复习现在看以前的代码又是豁然开朗。
- 前提:是一个maven项目或者普通Java项目
我的整体架构(简单版):
数据库sql
-- test.test definition
CREATE TABLE `test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
`u_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
编写配置文件sqlMapConfig.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>
<!-- 数据源环境配置,目前是需要的,整合spring之后,不需要这一块配置 -->
<environments default="development">
<environment id="development">
<!-- 事务的控制,选择默认的jdbc控制事务,整合spring之后,也不需要 -->
<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?useUnicode=true&characterEncoding=utf-8"/>
<!-- 用户名 -->
<property name="username" value="root"/>
<!-- 密码 -->
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--加载mapper.xml-->
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
实体类
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private String username;
private String password;
private int id;
private int uId;
}
mapper层
public interface UserMapper {
/**
* 根据id查询
* @param id
* @return
* @throws Exception
*/
public User queryUserById(int id) throws Exception;
/**
* 根据id修改
* @param id
* @return
* @throws Exception
*/
public int updataUserById(User user)throws Exception;
/**
* 新增
* @param user
* @return
* @throws Exception
*/
public int inserUser(User user)throws Exception;
/**
* 根据id删除
* @param id
* @throws Exception
*/
public int deleteUserById(int id) throws Exception;
/**
*
* 查询所有
* @return
* @throws Exception
*/
public List<User> selectAllUser() throws Exception;
编写xml
<?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.yaxin.mapper.UserMapper">
<select id="queryUserById" parameterType="integer" resultType="com.yaxin.model.User">
select * from test where id=#{id}
</select>
<update id="updataUserById" parameterType="com.yaxin.model.User">
update test set username=#{username},password=#{password},u_id=#{uId} where id=#{id}
</update>
<insert id="inserUser" parameterType="com.yaxin.model.User">
insert into test(username,password,u_id) values (#{username},#{password},#{uId})
</insert>
<delete id="deleteUserById" parameterType="integer">
delete from test where id=#{id}
</delete>
<select id="selectAllUser" resultType="com.yaxin.model.User">
select * from test
</select>
</mapper>
public class UserDaoImpl implements UserMapper {
//声明会话工厂对象
private SqlSessionFactory factory;
public UserDaoImpl() {
try {
String path = "SqlMapConfig.xml";
InputStream config = Resources.getResourceAsStream(path);
factory = new SqlSessionFactoryBuilder().build(config);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 根据id查询单个用户的值
* @param id
* @return
* @throws Exception
*/
@Override
public User queryUserById(int id) throws Exception {
//开启会话
SqlSession session = factory.openSession();
//获取mapper对象,名字和mapper接口名一致
UserMapper mapper = session.getMapper(UserMapper.class);
User user = mapper.queryUserById(id);
System.out.println("根据id查询成功"+user);
session.commit();
session.close();
return user;
}
@Override
public int updataUserById(User user) throws Exception {
//开启会话
SqlSession session = factory.openSession();
//获取mapper对象,名字和mapper接口名一致
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.updataUserById(user);
System.out.println("根据id修改成功"+i);
session.commit();
session.close();
return i;
}
@Override
public int inserUser(User user) throws Exception {
//开启会话
SqlSession session = factory.openSession();
//获取mapper对象,名字和mapper接口名一致
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.inserUser(user);
System.out.println("新增成功");
session.commit();
session.close();
return i;
}
@Override
public int deleteUserById(int id) throws Exception {
//开启会话
SqlSession session = factory.openSession();
//获取mapper对象,名字和mapper接口名一致
UserMapper mapper = session.getMapper(UserMapper.class);
int i = mapper.deleteUserById(id);
if(i>0){
System.out.println("根据id删除成功"+i);
}
session.commit();
session.close();
return i;
}
@Override
public List<User> selectAllUser() throws Exception {
//开启会话
SqlSession session = factory.openSession();
//获取mapper对象,名字和mapper接口名一致
UserMapper mapper = session.getMapper(UserMapper.class);
List<User> users = mapper.selectAllUser();
System.out.println("查询所有成功"+users);
session.commit();
session.close();
return users;
}
这个是对数据库操作,而且mybatis是一个持久层框架:对数据库操作就会产生事务 所有不要忘记记得commit和close关闭流
测试
// 测试
public static void main(String[] args) throws Exception {
// 根据id查询
new UserDaoImpl().queryUserById(1);
// 查询所有
new UserDaoImpl().selectAllUser();
// 根据id修改
User user = new User();
user.setId(1);
user.setPassword("12313");
user.setUsername("asdadas");
user.setUId(11);
new UserDaoImpl().updataUserById(user);
// 根据id删除
new UserDaoImpl().deleteUserById(1);
// 新增
User user = new User();
user.setUsername("xiao");
user.setPassword("2222");
user.setUId(2);
new UserDaoImpl().inserUser(user);
}
总结:
子曰:“温故而知新,可以为师矣。”学习的过程就是在不断的学习,不断的复习和公公知识。这样才能把知识为我所用。
|