当前位置: 首页 > news >正文

做宠物的网站主题思想电商运营学习网站

做宠物的网站主题思想,电商运营学习网站,流程优化,哪个网站可以找题目给小孩做该项目使用spring纯注解方式开发&#xff0c;用配置类取代spring的配置文件 一、导入依赖 整合Junit需要导入spring-test 整合mybatis需要导入spring-jdbc、mybatis-spring <dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-cont…

该项目使用spring纯注解方式开发,用配置类取代spring的配置文件

一、导入依赖

整合Junit需要导入spring-test
整合mybatis需要导入spring-jdbc、mybatis-spring

 <dependencies><!-- https://mvnrepository.com/artifact/org.springframework/spring-context --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.18</version></dependency><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.8</version></dependency><!-- https://mvnrepository.com/artifact/junit/junit --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.2</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-test --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.19</version><scope>test</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.48</version></dependency><!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>1.3.1</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.19</version></dependency></dependencies>

二、创建目录结构

  1. 配置类
  2. mapper接口
  3. service和serviceimpl
  4. pojo
    在这里插入图片描述5.在resource创建jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis01
jdbc.username=root
jdbc.password=root

三、创建spring配置类、mybatis配置类、jdbc配置类

SpringConfig.java
该配置类需要在类上方添加注解@Configuration表名自己是spring的配置类,还需要扫描添加了注解的包,即扫描bean@ComponentScan,以及导入properties配置文件跟别的配置类

@Configuration
@ComponentScan({"com.xs.mapper","com.xs.service"})
@Import({JDBCConfig.class,MyBatisConfig.class})
@PropertySource("classpath:jdbc.properties")
public class SpringConfig {}

MybatisConfig.java替代了mybatis的配置文件
导入第三方bean只需要在方法上方添加@Bean注解,这样就会将方法的返回值作为一个Bean。如果需要传别的bean的话,只需要在方法中写形参即可。

public class MyBatisConfig {/*** 配置sqlSessionFactory* 设置数据源* 设置别名* @param dataSource* @return*/@Beanpublic SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){SqlSessionFactoryBean sqlSessionFactory = new SqlSessionFactoryBean();sqlSessionFactory.setDataSource(dataSource);sqlSessionFactory.setTypeAliasesPackage("com.xs.pojo");return sqlSessionFactory;}/*** 扫描mapper接口* @return*/@Beanpublic MapperScannerConfigurer mapperScanner(){MapperScannerConfigurer msc = new MapperScannerConfigurer();msc.setBasePackage("com.xs.mapper");return msc;}}

JdbcConfig.java

public class JDBCConfig {@Value("${jdbc.driver}")private String  driver;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;@Beanpublic DataSource dataSource(){DruidDataSource druidDataSource = new DruidDataSource();druidDataSource.setDriverClassName(driver);druidDataSource.setUrl(url);druidDataSource.setUsername(username);druidDataSource.setPassword(password);return druidDataSource;}
}

四、创建pojo类和service、serviceimpl、mapper接口

其中需要注意的是serviceImpl中需要引入mapper接口。使用@AutoWire注解引入。这个注解是按类型匹配
Account.java类

public class Account {private Integer id;private String name;private Double money;get/set方法...有参无参构造函数...}

AccountMapper.java
注意:当传入多个参数时,需要添加@Param注解。该注解会用它的值作为传入mybatis’中的参数的值的名称。

@Repository
public interface AccountMapper {@Select("select * from t_account")public List<Account> accounts();@Select("select * from t_account where id=#{id}")public Account findById(Integer id);@Insert("insert into t_account(name,money) values(#{name},#{money})")public int addAccount(@Param("name") String name,@Param("money") double money);@Insert("insert into t_account(name,money) values(#{name},#{money})")public int addAccount2(Account account);@Update("update t_account set name=#{name},money=#{money} where id=#{id}")public int updateAccount(Account account);@Delete("delete from t_account where id=#{id}")public int delAccount(Integer id);
}

AccountService.java

public interface AccountService {
//    查询全部public List<Account> accounts();
//    按id查询public Account findById(Integer id);
//    增public int addAccount(String name,double money);
//    传入对象public int addAccount2(Account account);
//    改public int updateAccount(Account account);
//    删public int delAccount(Integer id);
}

AccountServiceImpl.java
该类继承了AccountService.java

@Service
public class AccountServiceImpl implements AccountService {
//    注入AccountMapper@Autowiredpublic AccountMapper accountMapper;public List<Account> accounts() {return accountMapper.accounts();}public Account findById(Integer id) {return accountMapper.findById(id);}public int addAccount(String name,double money) {return accountMapper.addAccount(name,money);}@Overridepublic int addAccount2(Account account) {return accountMapper.addAccount2(account);}public int updateAccount(Account account) {return accountMapper.updateAccount(account);}public int delAccount(Integer id) {return accountMapper.delAccount(id);}
}

五、进行测试

//设置类运行器
@RunWith(SpringJUnit4ClassRunner.class)
// 设置spring环境对应的配置类
@ContextConfiguration(classes = SpringConfig.class)
public class springMybatisTest {@Autowiredprivate AccountService accountService;
//    查询所有@Testpublic void accountsTest(){List<Account> accounts = accountService.accounts();accounts.forEach(account-> System.out.println(account));}
//    增加@Testpublic void addAccountTest(){
//        int i = accountService.addAccount("卫三",1000);int i = accountService.addAccount2(new Account(null, "应星决", 20000.0));System.out.println(i);}//    修改@Testpublic void updatrAccountTest(){Account account = new Account(9,"花花",10000.0);
//        account.setMoney(10000.0);
//        account.setName("熊二");int i = accountService.updateAccount(account);System.out.println(i);}//    根据id查询@Testpublic void selAccountTest(){Account i = accountService.findById(7);System.out.println(i);}
//    删@Testpublic void delAccount(){accountService.delAccount(8);}
}
http://www.yayakq.cn/news/28729/

相关文章:

  • flash网站链接怎么做深圳创业补贴去哪里申请
  • 加快政务网站建设求个网址老哥们2021
  • 河南移动官网网站建设做展会怎么引流到自己的网站
  • 枣庄网站开发公司网站导航栏全屏怎么做
  • 携程网站建设状况北京网站建设方案案例
  • 做电影视频网站赚钱嘛低代码开发工具
  • 重庆网站推广产品武安市精品网站开发
  • 爱站在线关键词挖掘广东官方移动网站建设哪家好
  • 申请免费网站域名网页升级紧急通知拿笔记好
  • 微信微网站是什么格式赣州市建设考勤网站
  • 网站设计与管理科技公司网站首页
  • 梅州市住房和城乡建设局网站广告平面设计公司
  • 做生存分析的网站网盟推广图片
  • wordpress建站linux广东线上营销推广方案
  • 如何把自己电脑做网站服务器吗网站建设名牌
  • 12306网站建设企业宣传片制作公司哪家好
  • 土木工程毕设代做网站北京城建建设工程有限公司
  • 学校网站建设机构怎样做网站软件
  • 做精美得ppt网站知乎免费网页制作软件下载
  • 如何撤销网站上信息吗天津网站优化公司电话
  • 四川圣泽建设集团有限公司网站代做ppt平台
  • 长沙做网站建设公司排名太原阳性确诊
  • 网站开发制作包括哪些的基本流程自己做企业网站
  • 广州网站制作品牌做外贸用什么软件找客户
  • 景区类网站互联网医院运营方案
  • 网站公司建设网站价格wordpress照片展示
  • 个人网站建设挂载下载链接沈阳公司网页制作
  • 这样自己做网站网站漂浮窗口代码
  • WordPress主题站公司网址正确格式
  • 看上去高端的网站新乡做网站推广的