spring整合mybatis、springmvc

 

 

目录结构:

我先创建实体类也就是domain下的Account

public class Account implements Serializable {
    private Integer id;
    private String name;
    private Double money;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }

编写AccountService接口和实现类AccountSerivceImpl

public interface AccountService {

    List<Account> findAll();
    Account findById(Integer id);
}
@Service("accountService")
public class AccountServiceImpl implements AccountService {

    private AccountDao accountDao;
    @Override
    public List<Account> findAll() {
        System.out.println("业务层查询所有用户");
        return null;
    }

    @Override
    public Account findById(Integer id) {
        return null;
    }
}

已经加上@Service注解

编写Controller前端控制器类

@Controller
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private AccountService accountService;

    @RequestMapping("/findAll")
    public String findAll(){
        System.out.println("表现层:查询所有用户");
        List<Account> all = accountService.findAll();
        for (Account account : all) {
            System.out.println(account);
        }
        return "list";
    }


}

编写application.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
	http://www.springframework.org/schema/beans/spring-beans.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!-- 只扫描service层实现类的包 -->
    <context:component-scan base-package="com.kt.Service.Impl"></context:component-scan>

    <!--spring整合mybatis框架-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://192.168.232.129:3306/www"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
        <!--配置sqlsession工厂对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--扫描AccountDao接口所在的包-->
    <context:component-scan base-package="com.kt.dao"></context:component-scan>
    <!--配置事务管理器-->
    <bean id="driverManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
   <!-- 配置事务的通知-->
    <tx:advice id="txManager" transaction-manager="driverManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true"/>
            <tx:method name="*" isolation="DEFAULT"/>
        </tx:attributes>
    </tx:advice>


    <!--配置AOP增强,为了引用事务通知-->
    <aop:config>
        <aop:advisor advice-ref="txManager" pointcut="execution( * com.kt.Service.Impl..*(..))"></aop:advisor>
    </aop:config>
</beans>

dataSource那些值可以写在一个properties文件里,但是我没写了。如果有人写了,记得在dataSource里使用el表达式,记得使用下命的标签引用外部文件

<!-- 引入外部配置文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值