发表于: 2019-10-29 23:51:12

1 753


今天完成的事情:


看了下spring 常用注解


@Repository  和 @Autowired

spring的注解作用是将一个类实例化后放入spring容器中,在我们需要它的时候,再通过Autowired取出

@ContextConfiguration


Spring其他常用注解:
@Component:是所有受Spring 管理组件的通用形式,@Component注解可以放在类的头上,@Component不推荐使用。
@Controller:对应表现层的Bean,也就是Action
@ Service:对应的是业务层Bean
@ Repository:标注一个DAO组件类


@RunWith  和  @ContextConfiguration





spring+mybatis 连接数据库


applicationContext 配置文件

    <context:component-scan base-package="com.ptteng"/>

   <!--数据源配置-->
   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="username" value="root"></property>
       <property name="password" value="451976"></property>
       <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property>
       <property name="Url" value="jdbc:mysql://localhost:3306/student?serverTimezone=UTC"></property>
   </bean>

   <!--mybatisspring的整合,不需要mybatis自己的配置映射文件-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <!--配置连接数据库数据源-->
       <property name="dataSource" ref="dataSource"/>
       <!--配置Mapper文件所在位置-->
       <property name="mapperLocations" value="classpath*:mapper.MyMapper.xml"/>
   </bean>

   <!--MapperScannerConfigurer将会扫描basePackage并自动装配-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.ptteng.StudentMapper"/>
       <!--<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>-->
   </bean>
</beans>


mapper映射文件

 <select id="selectStudentId"  parameterType="java.lang.Integer" resultType="enity.Student">
     select * from bj where Stunum = #{id}
</select>
<!--    //查找全部   输出类型必须为user定义类-->
   <select id ="selectStudent"    resultType="enity.Student">
     select * from bj
</select>

<!--    //插入单个   传入类型必须为包装型user定义类-->
   <insert id="insertStudent"    parameterType="enity.Student">
    insert into bj (name,qq,type,time,stunum) values (#{name},#{qq},#{type},#{time},#{stunum})
</insert>

<!--    //删除单个   传入类型必须为包装型user定义类-->
   <delete id="deleteStudentStunum"   parameterType="enity.Student" >
   delete from bj where stunum = #{stunum}
</delete>

<!--    //更改单个    传入类型必须为包装型user定义类-->
   <update id="updateStudent"  parameterType="enity.Student" >
   update bj set name = #{name} where id = #{id}
</update>

实体文件 student     省略set  get

接口文件 StudentMapper 

@Repository
public interface StudentMapper {

public void selectStudentId();

public void selectStudent();

public void insertStudent();

public void deleteStudentStunum();

public void updateStudent();

}


测试文件  Student   

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("/applicationContext.xml")
public class StudentTest {

@Autowired
       StudentMapper mapper;
       Student student = new Student();

       @Test
       public void addStudent() {
student.setName("小李");
           student.setqq(2134);
           student.setType("后端");
           student.settime("人生");
           System.out.println(student.getId());


搭建还未完成  明天继续


明天计划的事情:

搭建spring+mybatis    

写单元测试


遇到的问题:

对spring整合mybatis具体原理

还是有点模糊


收获:

对spring的一些注解更了解了些


返回列表 返回列表
评论

    分享到