<dependencies>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.4.6version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
mybatis的全局配置文件,配置了mybatis的运行环境等信息。
<configuration>
<properties resource="db.properties">properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${db.driver}" />
<property name="url" value="${db.url}" />
<property name="username" value="${db.username}" />
<property name="password" value="${db.password}" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="UserMapper.xml" />
mappers>
configuration>
db.properties
db.driver=com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost:3306/shop
db.username = root
db.password=root
User.java实体类
public class User {
int id;
String username;
String password;
String email;
String phone;
String address;
//省略toString,get,set
新建user表
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`address` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8
UserMapper.xml
<mapper namespace="test">
<select id="findUserById" parameterType="int"
resultType="User">
SELECT * from user WHERE id=#{id}
select>
mapper>
public class MyBatisTest {
private SqlSessionFactory sqlSessionFactory;
@Before
public void creatSqlSessionFactory() throws IOException {
String resource = "SqlMapConfig.xml";
// 读取SqlMapConfig文件
InputStream in = Resources.getResourceAsStream(resource);
//创建sqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}
@Test
public void testFindUserById(){
SqlSession sqlSession =null;
try {
// 获取SqlSeesion
sqlSession = sqlSessionFactory.openSession();
// 查询
User user = sqlSession.selectOne("test.findUserById",1);
// 输出
System.out.println(user);
}catch (Exception e){
e.printStackTrace();
}finally {
if (sqlSession!=null)
sqlSession.close();
}
}
}
输出结果
User{id=1, username='aaa', password='aaa', email='[email protected]', phone='aaa', address='aaa'}
#{}:
${}
对象导航图语言
|--user(参数值对象)
|--username--测试
|--password--123
|--depa--Department
|--name--测试
|--number--121
<selectKey keyProperty="id" order="AFTER" resultType="int">
select Last_INSERT_ID()
selectKey>
keyProperty:指定返回的主键存储在pojo中的那个属性
order:selectKey标签中的sql执行顺序,是相对与insert语句来说的,由于mysql的自增原理。执行完insert语句之后才将主键生成,所以这里selectKey的执行顺序为after
UserMapper.xml
<insert id="addUser" parameterType="User">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
selectKey>
INSERT INTO user(username,password,email,phone,address)
VALUES (#{username},#{password},#{email},#{phone},#{address})
insert>
test
@Test
public void testAddUser(){
SqlSession sqlSession =null;
try {
sqlSession = sqlSessionFactory.openSession();
User user = new User("小小", "111", "1@com", "123", "sasas");
int count =sqlSession.insert("test.addUser", user);
System.out.println(count);
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
if (sqlSession!=null)
sqlSession.close();
}
}
UserMapper.xml
<delete id="deleteUserById" parameterType="int">
DELETE FROM user WHERE id =#{id}
delete>
@Test
public void testDeleteUser(){
SqlSession sqlSession =null;
try {
sqlSession = sqlSessionFactory.openSession();
int count =sqlSession.delete("test.deleteUserById", 13);
System.out.println(count);
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
if (sqlSession!=null)
sqlSession.close();
}
}
UserMapper.xml
<update id="updateUser" parameterType="User">
UPDATE user set username=#{username},email=#{email},phone=#{phone},address =#{address}
WHERE id =#{id}
update>
@Test
public void testUpdateUser(){
SqlSession sqlSession =null;
try {
sqlSession = sqlSessionFactory.openSession();
User u =new User("修改","1234","test@","121212","");
u.setId(1);
int count =sqlSession.update("test.updateUser", u);
System.out.println(count);
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
}finally {
if (sqlSession!=null)
sqlSession.close();
}
}