maven依赖 :
https://mvnrepository.com/
1.配置mybatis-config.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
>
<!-- 别名 -->
<
typeAliases
>
<
package
name=
"com.test2"
/>
</
typeAliases
>
<!-- 数据库环境 -->
<
environments
default=
"development"
>
<
environment
id=
"development"
>
<
transactionManager
type=
"JDBC"
/>
<
dataSource
type=
"POOLED"
>
<
property
name=
"driver"
value=
"com.mysql.jdbc.Driver"
/>
<
property
name=
"url"
value=
"jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8"
/>
<
property
name=
"username"
value=
"root"
/>
<
property
name=
"password"
value=
"root"
/>
</
dataSource
>
</
environment
>
</
environments
>
<!-- 映射文件 也就是数据库要操作那些个表-->
<
mappers
>
<
mapper
resource=
"./student.xml"
/>
</
mappers
>
</
configuration
>
2.配置要操作的数据表文件 student.xml
<?
xml
version=
"1.0"
encoding=
"UTF-8"
?>
<!
DOCTYPE
mapper
PUBLIC
"-//mybatis.org//DTD Mapper 3.0//EN"
<!-- 进行操作当前表 -->
<
mapper
namespace=
"test2"
>
<!-- parameterType:参数类型 resultType:返回的结果类型 -->
<
select
id=
"listStudent"
resultType=
"Student"
>
select * from student
</
select
>
<
insert
id=
"addStudent"
parameterType=
"Student"
>
insert into student (
sid, name, sex) values (#{
sid},#{name},#{sex})
</
insert
>
<
delete
id=
"deleteStudent"
parameterType=
"_int"
>
delete from student where
sid = #{
sid}
</
delete
>
<
select
id=
"getStudent"
parameterType=
"_int"
resultType=
"Student"
>
select * from student where
sid = #{
sid}
</
select
>
<
update
id=
"updateStudent"
parameterType=
"Student"
>
<!-- 同时修改两个属性 -->
update student set name=#{name} ,sex = #{sex} where
sid = #{
sid}
</
update
>
</
mapper
>
3.进行操作数据库
public
static
void main(String
args[]) {
// 根据
mybatis
-config.xml 配置的信息得到 sqlSessionFactory
String
resource =
"mybatis-config.xml";
InputStream
inputStream =
null;
try {
inputStream = Resources.
getResourceAsStream(
resource);
SqlSessionFactory
sqlSessionFactory =
new SqlSessionFactoryBuilder().build(
inputStream);
// 通过session进行数据库的操作
// 这里使用true表示的是要提交事务,如果不添加参数,那么默认是不提交事务的
// 也就是说不真正的修改数据库
// SqlSession session = sqlSessionFactory.openSession(true);
// 现在测试阶段,不真正的修改数据库
SqlSession
session =
sqlSessionFactory.openSession();
Student
student1 =
new Student();
student1.setSid(18);
student1.setName(
"大美妞");
student1.setSex(
"妞");
// 进行增删改查的时候,在第一个参数中填入的是你在
xml
中配置的查询语句的名称
//增
session.insert(
"addStudent",
student1);
//删
session.delete(
"deleteStudent", 0);
//查
Student
selectOne =
session.selectOne(
"getStudent", 18);
System.
out.println(
"选择一个==" +
selectOne.toString());
//更新
Student
student2 =
new Student();
student2.setSid(5);
student2.setName(
"大妞");
student2.setSex(
"很不错");
session.update(
"updateStudent",
student2);
List<Student>
listStudent =
session.selectList(
"listStudent");
for (Student
student :
listStudent) {
System.
out.println(
student.toString());
}
}
catch (IOException
e) {
//
TODO
Auto-generated catch block
e.printStackTrace();
}
}