Mybatis 的使用 (1)

1、首先创建Maven工程并倒入需要的jar包(pom.xml中配置):

<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.5</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-mxj -->

            <dependency>
                      <groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.39</version><!--$NO-MVN-MAN-VER$ -->
		</dependency>

2、创建Mybatis配置文件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>
<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://127.0.0.1:3306/mybatis_demo" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mybatis/entity/UserMapper.xml"/>
</mappers>

</configuration>

3、创建实体类以及对应的数据库表格:

实体类(其余方法自己补充):

public class User {

        private Integer id;

        private String name;

private Integer age;

private String address;

}

表格:

CREATE TABLE `t_user` (
  `id` int(10) NOT NULL auto_increment,
  `name` varchar(50) NOT NULL,
  `age` int(2) default NULL,
  `address` varchar(128) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8


4、编写操作数据的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="mybatis.entity.User">
<select id="queryUserById" resultType="mybatis.entity.User">
select * from t_user where id=#{id}
</select>
<select id="queryUsers" resultType="mybatis.entity.User">
select * from t_user
</select>
<insert id="insertUser" parameterType="mybatis.entity.User">
insert into t_user(name,age,address) 
values
(#{name},#{age},#{address})
</insert>
<update id="modifyUser" parameterType="mybatis.entity.User">
update t_user
set name = #{name},
age = #{age},
address = #{address}
where id = #{id}
</update>
<delete id="deleteUser" >
delete from t_user where id=#{id}
</delete>

</mapper>

5、最后编写测试类:

public class UserMethodTest {
SqlSession sqlSession = null;
public static final String NAMESPACE="mybatis.entity.User";

@Before
public void prepareResource() throws IOException {
Reader configReader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(configReader);
sqlSession = sessionFactory.openSession();
}
@Test
@Ignore
public void testQuery() {
User user = sqlSession.selectOne(NAMESPACE+".queryUserById", 1);
System.out.println("user is :"+user);

sqlSession.close();
}
@Ignore
@Test
public void testQueryList() {
List<User> userList = sqlSession.selectList(NAMESPACE+".queryUsers");
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
@Test
@Ignore
public void insertUser(){
User user = new User("wangwu", 33, "shanghai");
int insert = sqlSession.insert(NAMESPACE+".insertUser", user);
sqlSession.commit();
System.out.println(insert);
sqlSession.close();
}
@Test
@Ignore
public void modifyUser(){
User user = new User("王五", 33, "上海");
user.setId(4);
int insert = sqlSession.update(NAMESPACE+".modifyUser", user);
sqlSession.commit();
System.out.println(insert);
sqlSession.close();
}
@Test
public void deleteUser(){
int delete = sqlSession.delete(NAMESPACE+".deleteUser", 4);
sqlSession.commit();
System.out.println(delete);
sqlSession.close();
}
}

阅读更多
文章标签: mybati
个人分类: Mybatis
上一篇微信小程序访问豆瓣电影API 403 400
下一篇WebMvcConfigurerAdapter 在Springboot 2.0已被废弃
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭