Mybatis基本实例详解
Mybatis是一款开源的持久化框架,它可以将数据库的操作和Java代码解耦,大大简化了数据访问层的开发。本文将介绍Mybatis基本实例,包含如下内容:
- Mybatis简介
- Mybatis基本配置
- Mybatis增删改查示例1
- Mybatis增删改查示例2
Mybatis简介
Mybatis是一款优秀的持久层框架,它为Java开发提供了一个优秀的数据访问解决方案。Mybatis通过XML或注解的方式配置SQL映射,从而将Java对象与数据库表进行映射,实现了轻量级的数据访问层框架。
Mybatis基本配置
Mybatis基本配置主要包括配置数据源、配置SqlSessionFactory和SqlSessionFactoryBean,以及配置Mapper文件路径等。下面给出一个基本的Mybatis配置文件示例:
<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.example.entity"/>
<property name="mapperLocations">
<array>
<value>classpath:mapper/*.xml</value>
</array>
</property>
</bean>
<!--配置Mapper扫描-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.example.mapper"/>
</bean>
配置完成后,我们就可以开始编写Mapper接口和Mapper.xml文件了。
Mybatis增删改查示例1
下面给出一个基本的Mybatis增删改查示例:
Mapper接口
public interface UserMapper {
void insert(User user);
void deleteById(Long id);
void update(User user);
User selectById(Long id);
}
Mapper.xml文件
<mapper namespace="com.example.mapper.UserMapper">
<insert id="insert" parameterType="com.example.entity.User">
insert into user (id, name, age) values (#{id}, #{name}, #{age})
</insert>
<delete id="deleteById" parameterType="java.lang.Long">
delete from user where id = #{id}
</delete>
<update id="update" parameterType="com.example.entity.User">
update user set name=#{name}, age=#{age} where id=#{id}
</update>
<select id="selectById" parameterType="java.lang.Long" resultType="com.example.entity.User">
select id, name, age from user where id = #{id}
</select>
</mapper>
测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Test
public void testInsert() {
User user = new User();
user.setId(1L);
user.setName("test");
user.setAge(20);
userMapper.insert(user);
}
@Test
public void testDeleteById() {
userMapper.deleteById(1L);
}
@Test
public void testUpdate() {
User user = new User();
user.setId(1L);
user.setName("test2");
user.setAge(22);
userMapper.update(user);
}
@Test
public void testQueryById() {
User user = userMapper.selectById(1L);
System.out.println(user);
}
}
Mybatis增删改查示例2
下面给出一个基本的Mybatis增删改查示例:
Mapper接口
public interface ArticleMapper {
void insert(Article article);
void deleteById(Long id);
void update(Article article);
Article selectById(Long id);
List<Article> selectAll();
}
Mapper.xml文件
<mapper namespace="com.example.mapper.ArticleMapper">
<insert id="insert" parameterType="com.example.entity.Article">
insert into article (id, title, content) values (#{id}, #{title}, #{content})
</insert>
<delete id="deleteById" parameterType="java.lang.Long">
delete from article where id = #{id}
</delete>
<update id="update" parameterType="com.example.entity.Article">
update article set title=#{title}, content=#{content} where id=#{id}
</update>
<select id="selectById" parameterType="java.lang.Long" resultType="com.example.entity.Article">
select id, title, content from article where id = #{id}
</select>
<select id="selectAll" resultType="com.example.entity.Article">
select id, title, content from article
</select>
</mapper>
测试代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class ArticleMapperTest {
@Autowired
private ArticleMapper articleMapper;
@Test
public void testInsert() {
Article article = new Article();
article.setId(1L);
article.setTitle("test");
article.setContent("test content");
articleMapper.insert(article);
}
@Test
public void testDeleteById() {
articleMapper.deleteById(1L);
}
@Test
public void testUpdate() {
Article article = new Article();
article.setId(1L);
article.setTitle("test2");
article.setContent("test2 content");
articleMapper.update(article);
}
@Test
public void testQueryById() {
Article article = articleMapper.selectById(1L);
System.out.println(article);
}
@Test
public void testQueryAll() {
List<Article> articles = articleMapper.selectAll();
System.out.println(articles);
}
}
以上两个示例展示了基本的增删改查操作,通过配置Mapper接口和Mapper.xml文件,以及进行简单的测试,可以让我们更好地了解Mybatis的基本使用。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:mybatis基本实例详解 - Python技术站