spring和mybatis整合

spring和mybatis整合

         mybatis的前身即ibatis,两者之间有较大的差别,这里主要说mybatis,从配置上来看,mybatis很像hibernate的配置,或许是开发者有意为之吧,用过hibernate的人也更加容易入手。
首先,先创建一张表,插入两条数据:

create tableuser_c(id int primary key not null,name varchar(50),age int,address varchar(200));
insert into
user_c values(1,'张三',22,'公司')
insert into
user_c values(2,'李四',22,'村儿里')

接着就是代码部分的书写了,项目目录和用到的jar包如下

先在model目录下写一个实体类User
public class User {
private int id;
private String name;
private int age;
private String address;

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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public User() {
super();
}
public User(int id, String name, int age, String address) {
super();
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
}

接着写Dao,是一接口,这些接口的方法名要跟下面的mapper文件的id名保持一致这样才能做到一一对应,下面是UserDao部分的代码:
public interface UserDao {
public User getUserById(int id);


public void insertUser(User user);


public void updateUser(User user);


public void deleteUser(int id);


public List<User> getAllUser();
}

接着是跟UserDao对应的UserDaoMapper.xml文件,用于书写sql语句:
<?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="com.springmybatis.test.dao.UserDao">
<!--  新增记录  -->
    <insert id="insertUser" parameterType="com.springmybatis.test.model.User" useGeneratedKeys="true" keyProperty="id">
        insert into user_c(id,name,age,address) values(#{id},#{name},#{age},#{address})
    </insert>
<!--  查询单条记录  -->
    <select id="getUserById" parameterType="int" resultType="com.springmybatis.test.model.User">
       select * from user_c where id = #{id}
    </select>
<!--  修改记录  -->
    <update id="updateUser" parameterType="com.springmybatis.test.model.User">
        update user_c set name = #{name} where id = #{id}
    </update>
<!--  查询所有记录  -->
    <select id="getAllUser" resultType="com.springmybatis.test.model.User">
        select * from user_c
    </select>
<!--  删除记录  -->
    <delete id="deleteUser" parameterType="int">
       delete from user_c where id = #{id}
    </delete>
   
</mapper>

基本上测试的话上面的代码就可以了,剩下的就是配置文件的书写了,但是这里习惯性的写了个service层,UserService文件的代码如下:
@Service
public class UserService {

@Autowired
UserDao userDao;

public User getUserById(int id) {

return userDao.getUserById(id);
}

public void insertUser(User user) {
userDao.insertUser(user);


}

public void updateUser(User user) {
System.out.println("sssssssssssssssss");
userDao.updateUser(user);
}

public void deleteUser(int id) {
userDao.deleteUser(id);
}

public List<User> getAllUser() {
return userDao.getAllUser();
}

}

下面写spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

 <context:property-placeholder location="classpath:jdbc.properties" />  
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
       <property name="driverClassName" value="${jdbc.driver}" />
       <property name="url" value="${jdbc.url}" />
       <property name="username" value="${jdbc.username}" />
       <property name="password" value="${jdbc.password}" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="dataSource" ref="dataSource" />  
    <!-- <property name="configLocation" value="mybatis-config.xml"></property> -->
    <property name="mapperLocations" value="classpath:com/springmybatis/test/dao/*.xml"></property>  
</bean>  


<!-- 下面注释的内容适合Dao比较少的情况,如果有多个Dao还去这样写就显得很麻烦 -->
<!-- <bean class="org.mybatis.spring.mapper.MapperFactoryBean">  
    <property name="mapperInterface" value="com.springmybatis.test.dao.UserDao"></property>  
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>  
</bean> -->


<!-- 下面这样的配置其实也是扫描包,直接扫描存放Dao的包就好了,如果有很多的Dao用这样的配置 -->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
       <property name="basePackage" value="com.springmybatis.test.dao" />  
    </bean>  


<context:component-scan base-package="com.springmybatis.test.dao" />
<context:component-scan base-package="com.springmybatis.test.service" />
 


</beans>

使用mybatis,也要有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>  
    <mappers>  
        <mapper resource="com/springmybatis/test/dao/UserMapper.xml"/>  
    </mappers>  
</configuration>

最后就可以写测试类来测试,程序能否正常跑起来了,测试类如下:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:/applicationContext.xml" })
public class UserController {


@Autowired
UserService userService;


@Test
public void test1() {
User user = userService.getUserById(1);
System.out.println(user.getName());
}


@Test
public void main() {
User user = new User();
// 修改数据
user.setId(1);
user.setName("强强5");
user.setAge(12);
user.setAddress("fffffffffffffffffffff");
userService.updateUser(user);
// 删除数据
userService.deleteUser(2);
System.out.println("删除成功");
}
}

右键----》Run as ---->junit test就可以了



已标记关键词 清除标记
相关推荐
<p style="color:#424242;font-size:14px;"> 全程实战讲解,附<strong>源码 数据库脚本</strong>和<strong>项目素材</strong> </p> <p style="color:#424242;font-size:14px;"> <strong>核心技术</strong>:SpringBoot+SpringMvc+Spring+MyBatis+Mysql+Thymeleaf+Jquery+Ajax+HTML+CSS,使用Maven完成项目的构建! </p> <p style="color:#424242;font-size:14px;"> <br /></p> <p style="color:#424242;font-size:14px;"> <strong>项目前台点餐系统部分截图:</strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021001262373.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021001389462.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021001535188.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021002057197.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><span style="font-size:16px;">项目后台管理系统部分截图:</span><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021002198435.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021002335651.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021002445054.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021002548248.png" alt="" /><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><br /></strong> </p> <p style="color:#424242;font-size:14px;"> <strong><img src="https://img-bss.csdn.net/202003021003138072.png" alt="" /><br /></strong> </p>
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页