小插曲:
连接数据库报错Can’t connect to local MySQL server through socket /tmp/mysql.sock,看了很多解决方法都跟自身情况不符,结果点了一下 系统偏好设置——下面MySQL服务——Initialize DataBase——Use Legacy Password——设置新密码——OK,就好了。。
准备
项目目录:
pojo类三个属性,空参有参构造,set get方法:
数据库表:
dao层接口4个抽象方法:
service层接口抽象方法同dao层。
service层实现类,声明dao对象,定义set方法用于注入,实现的方法通通调用dao对象的:
mybatis映射配置文件AccountDao.xml:
(注意:namespace与dao接口的全类名一致,select标签的id与dao接口的方法名一致,另外sql语句里的表名tb_account不要写错!)
<?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.dd.sm.dao.AccountDao">
<!--配置根据id查询-->
<select id="findById" resultType="account" parameterType="int">
select * from tb_account where id = #{id}
</select>
<!--配置查询所有-->
<select id="findAll" resultType="account">
select * from tb_account
</select>
<!--配置保存-->
<insert id="save" parameterType="account">
insert into tb_account(name,money)values(#{name},#{money})
</insert>
<!--配置删除-->
<delete id="delete" parameterType="int">
delete from tb_account where id = #{id}
</delete>
<!--配置更新-->
<update id="update" parameterType="account">
update tb_account set name=#{name},money=#{money} where id=#{id}
</update>
</mapper>
jdbc配置文件:
(注意url的写法,jdbc:mysql://IP:端口/数据库名!)
spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!--添加命名空间context,添加两个context的url-->
<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"
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">
<!--加载配置文件-->
<context:property-placeholder location="classpath:db.properties"/>
<!--管理数据源对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
<!--管理service实现类对象-->
<bean id="accountService" class="com.dd.sm.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--管理 创建sql会话的对象-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="typeAliasesPackage" value="com.dd.sm.pojo"/>
</bean>
<!--管理 扫描映射配置文件的对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.dd.sm.dao"/>
</bean>
</beans>
Servlet中通过spring调用service实现类对象:
运行输出:
spring注解
作用:代替spring配置文件中的<bean>标签。
原始注解:
@Component表示将这个资源交给spring管理。
@Controller、@Service、@Repository由@Component衍生而来,用于不同层,有了语义而已。
只用@Autowired是根据数据类型从spring容器里匹配资源,如果同一类型有多个资源,要用资源名称匹配,写两个注解:
或写一个:
@Value用法:读取配置文件:
使用注解:(set方法貌似也不需要了)
spring配置文件:
配置让spirng扫描注解的路径:
使用了注解的资源,bean就不需要了:
spring新注解
原始注解的局限:
上面的原始注解不能完全替代xml,如非自定义对象dataSource:
新注解:
@Bean用法:
想把某个第三方对象管理到spring容器中,就定义一个类,类里定义一个返回该对象的方法,在方法上加@Bean,如果后面要注入这个资源,就七起个名称@Bean("XXX"),如果只是交给spring管理,就不需要起名。
全注解开发:
将spring配置文件、mybatis映射配置文件的全部配置项用注解替代。
mybatis映射配置文件:
注解代替:
spring配置文件
新建多个配置类:
——SpringDataSourceConfig——
标签:
用注解替代:
标签:
用注解替代:
——SpringMybatisConfig——
标签:
用注解替代:
——SpringConfig——
标签:
用注解替代:
——另外——
标签:
用注解替代:
到此为止,实现了全注解开发。
spring整合JUnit
pom文件:
新建测试类:
右击运行: