【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>
第一步:新建一个工程,导入这两个jar
第二步:在mysql下面新建数据库和表,由于是用来做学习,字段可以简单点
这是项目结构
第三步:新建实体类User
package com.ll.model;
public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
第四步:新建mapper文件userMapper.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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="com.ll.model.userMapper"就是com.ll.model.(包名)+userMapper(userMapper.xml文件去除后缀)
-->
<mapper namespace="com.ll.mapping.userMapper">
<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复
使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="com.ll.model.User"就表示将查询结果封装成一个User类的对象返回
User类就是users表所对应的实体类
-->
<select id="getUser" parameterType="int"
resultType="com.ll.model.User">
select * from users where id=#{id}
</select>
<insert id="addUser" parameterType="com.ll.model.User">
insert into users(name,age) values(#{name},#{age})
</insert>
<delete id="deleteUser" parameterType="int">
delete from users where id=#{id}
</delete>
<update id="updateUser" parameterType="com.ll.model.User">
update users set name=#{name},age=#{age} where id=#{id}
</update>
<select id="getAllUser" resultType="com.ll.model.User">
select * from users
</select>
</mapper>
第五步:新建配置文件conf.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>
<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/mybatis" />
<property name="username" value="root" />
<property name="password" value="password" />
</dataSource>
</environment>
</environments>
<mappers>
<!-- 注册userMapper.xml文件,
userMapper.xml位于com.ll.mapping这个包下,所以resource写成com/ll/mapping/userMapper.xml-->
<mapper resource="com/ll/mapping/userMapper.xml"/>
</mappers>
</configuration>
第六步:新建测试类
package com.ll.test;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.ll.model.User;
public class UnionTest {
public static void main(String[] args) {
//mybatis的配置文件
String resource = "conf.xml";
//使用类加载器加载mybatis的配置文件(它也加载关联的映射文件)
InputStream is = UnionTest.class.getClassLoader().getResourceAsStream(resource);
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
//使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件)
//Reader reader = Resources.getResourceAsReader(resource);
//构建sqlSession的工厂
//SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
//注意这里的true,是指自动提交,如果不写只能手动进行提交
SqlSession session = sessionFactory.openSession(true);
/**
* 映射sql的标识字符串,
* me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值,
* getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL
*/
/* String statement = "com.ll.mapping.userMapper.getUser";//映射sql的标识字符串
//执行查询返回一个唯一user对象的sql
User user = session.selectOne(statement, 1);
System.out.println(user);
*/
//获取所有数据
String statement = "com.ll.mapping.userMapper.getAllUser";
List<User> list=new ArrayList<User>();
list=session.selectList(statement);
for(int i=0;i<list.size();i++){
User u=list.get(i);
System.out.println(u);
}
//插入数据
/* String statement = "com.ll.mapping.userMapper.addUser";//映射sql的标识字符串
User user=new User();
user.setAge(10);
user.setName("yangyang");
int retResult = session.insert(statement, user);
session.close();
System.out.println(retResult);*/
//删除数据
/* String statement = "com.ll.mapping.userMapper.deleteUser";//映射sql的标识字符串
int retResult = session.delete(statement, 1);
session.close();
System.out.println(retResult);*/
//更新数据
/*String statement = "com.ll.mapping.userMapper.updateUser";//映射sql的标识字符串
User user=new User();
user.setAge(10);
user.setName("yangyang");
user.setId(2);
int retResult = session.update(statement,user);
session.close();
System.out.println(retResult);*/
}
}
来源:oschina
链接:https://my.oschina.net/u/1410278/blog/705340