一、Mybatis第一个项目
1、工程Maven配置文件:加入mysql依赖、mybatis依赖、junit依赖,build中配置resources防止资源导出失败
<!--导入依赖-->
<dependencies>
<!--mysql驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<!--在build中配置resources,来防止我们资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resoures</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
</project>
2、模块的Maven配置文件
同上加入build,注意该工程的模块下的配置文件都存在继承父文件的代码(如下)-自动生成
<parent>
<artifactId>Mybatis-study</artifactId>
<groupId>com.GCompanynode</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
3、模块的resources文件(核心配置文件)仲的数据库连接配置
注意:每一个Mapper.xml都需要在Mybatis核心配置文件中注册!!!
<!--核心配置文件-->
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT%2B8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--每一个Mapper.xml都需要在Mybatis核心配置文件中注册!!!-->
<mappers>
<mapper resource="com/GHcode/dao/UserMapper.xml"/>
</mappers>
4、创建实体类(包含无参、有参,setting and getting,toString)
public class User private int id; private String name; private String pwd;
5、创建实体类Maapper接口(相当于dao接口与数据库连接实现数据库的调用)
public interface UserMapper { List<User> getUserList(); }
6、mapper配置文件的建立(注意与上一步的配合)
*指定命名空间namespace:包名要和Dao/Mapper接口的包名一致!!
<!--namespace绑定一个指点的Dao/Mapper接口-->
<mapper namespace="com.GHcode.dao.UserMapper">
<!--查询语句-->
<select id="getUserList" resultType="com.GHcode.pojo.User">
select * from mybatis.user;
</select>
</mapper>
7、创建工具类(可提前设置)
获取SqlSessionFactory对象
//工具类
//获取SqlSessionFactory对象
public class MybaatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//获取SqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
8、测试类
/第一步、获取sqlsession
SqlSession sqlSession = MybaatisUtils.getSqlSession();
try{
//方式一:getMapper(推荐)
UserDao mapper = sqlSession.getMapper(UserMapper.class);
List<User> userList = mapper.getUserList();
//方式二:(不推荐)
/* List<User> UserList = sqlSession.selectList("com.GHcode.dao.UserMapper.getUserList");
for (User user : UserList) {
System.out.println(user);
}*/
}finally {
//关闭sqlsession
sqlSession.close();
}
============================分隔符========================================
二、实现增删改
只需要在三个文件中修改代码即可:
UerMapper.java接口
//根据id查询用户
User getUserById(int uid);
//增加用户
int addUser(User user);
//修改用户
int updateUser(User user);
//删除用户
int deleteUser(int id);
UserMapper.xml文件
<select id="getUserById" parameterType="int" resultType="com.GHcode.pojo.User">
select * from mybatis.user where id = #{param}
</select>
<insert id="addUser" parameterType="com.GHcode.pojo.User">
insert into mybatis.user(id, name, pwd) values (#{id},#{name},#{pwd});
</insert>
<update id="updateUser" parameterType="com.GHcode.pojo.User">
update mybatis.user set name=#{name},pwd=#{pwd} where id=#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id};
</delete>
模糊查询
/** 模糊查询的两个方式
* ① 传递通配符%
* Mapper配置文件:select * from mybatis.user where name like #{value}
* test类:List<User> ulist = mapper.getUserLike("%大%");
* ② 在sql拼接中使用通配符
* Mapper配置文件:select * from mybatis.user where name like "%"#{value}"%"
* test类:List<User> ulist = mapper.getUserLike("大");
*
*/
~企业中的使用:
通过map增加对象
<insert id="addUser2" parameterType="map">
insert into mybatis.user(id, name, pwd) values (#{userid},#{username},#{userpwd});
</insert>
Map<String, Object> map = new HashMap<String, Object>();
//String的内容需要与xml文件上的#{}中的参数名字相同
map.put("userid",6);
map.put("username","唐子");
map.put("userpwd","258741369");
mapper.addUser2(map);
注意:values后的参数名称要与map的键名相对应,不然无法获取内容
test测试类
重要*:进行增删改时主要提交事务 sqlSession.commit();
SqlSession sqlSession = MybaatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int res = mapper.updateUser(new User(10,"大飞","123123"));
if (res>0){
System.out.println("修改完成");
}
sqlSession.commit();
sqlSession.close();
}
==============================分隔线===================================