文件目录总览
在maven中添加依赖
<!--加入mybatis的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<!--加入mysql驱动的依赖,注意要和本机使用的数据同属于同一个版本-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!--告诉maven打包的时候把src/main/java里面所有的东西都打包,如果没有此配置,resources文件夹下的内容不会被打包-->
<build>
<resources>
<resource>
<directory>src/main/java</directory><!--所在的目录-->
<includes><!--包括目录下的.properties,.xml 文件都会扫描到-->
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<!--filtering 选项 false 不启用过滤器, *.property 已经起到过滤的作用了 -->
<filtering>false</filtering>
</resource>
</resources>
</build>
创建Student JavaBean类,注意属性名要和所要查询表的列名一致
在dao层下面创建mapper映射文件StudentDao.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:必须有值,推荐使用dao接口的全限定名称-->
<mapper namespace="club.soulhotel.dao.StudentDao">
<!--
id:推荐使用dao接口中方法名称,这样比较清晰
resultType:表示查询结果返回的数据类型,要是自定义对象的话,必须要使用对象的全限定名称。
-->
<select id="selectStudents" resultType="club.soulhotel.domain.Student">
select id, name, email, age from student order by id
</select>
<insert id="insertStudent">
insert into student values(#{id}, #{name}, #{email}, #{age})
</insert>
<update id="updateStudent">
update student set age = #{age} where id=#{id}
</update>
<delete id="deleteStudent">
delete from student where id=#{studentId}
</delete>
</mapper>
在resources文件目录下创建mybatis.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>
<!--加入日志配置,可以在控制台输出执行的sql语句和参数,注意要在前面声明,否则会报错-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!--default:默认使用哪个数据源-->
<environments default="mysql">
<!--id:数据源的名称-->
<environment id="mysql">
<!--事务类型:使用JDBC事务-->
<transactionManager type="JDBC"/>
<!--数据源:dataSource:创建数据库Connection对象
type:POOLED使用数据库的连接池-->
<dataSource type="POOLED">
<!--连接数据库的四个要素-->
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://阿里云数据库外网地址/springdb"/>
<property name="username" value="用户名"/>
<property name="password" value="密码"/>
</dataSource>
</environment>
</environments>
<mappers>
<!--告诉mybatis要执行的sql语句的位置,注意是文件的地址-->
<mapper resource="club\soulhotel\dao\StudentDao.xml"/>
</mappers>
</configuration>
创建Test类
public class Test {
public static void main(String[] args) throws IOException {
String config = "mybatis.xml";
InputStream in = Resources.getResourceAsStream(config);
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSession sqlSession = factory.openSession();
/*
查询操作
sqlId就是StudentDao.xml文件中的namespace+你所要使用的操作的id
*/
String sqlId = "club.soulhotel.dao.StudentDao" + '.' + "selectStudents";
List<Student> studentList = sqlSession.selectList(sqlId);
for(Student student : studentList) {
System.out.println(student);
}
/*
插入操作
*/
sqlId = "club.soulhotel.dao.StudentDao" + '.' + "insertStudent";
Student student = new Student();
student.setId(1009);
student.setName("赵六");
student.setEmail("zhaoliu@gmail.com");
student.setAge(25);
int nums = sqlSession.insert(sqlId, student);
/*注意,insert操作要手动调用commit()方法*/
sqlSession.commit();
/*
改操作
*/
sqlId = "club.soulhotel.dao.StudentDao" + '.' + "updateStudent";
student = new Student();
student.setId(1002);
student.setAge(30);
int rows = sqlSession.update(sqlId, student);
sqlSession.commit();
/*
删除操作
*/
int id = 1001;
rows = sqlSession.delete("club.soulhotel.dao.StudentDao.deleteStudent", id);
sqlSession.commit();
sqlSession.close();
}
}